Whamcloud - gitweb
LU-3963 client: move llite,lov,target,obdecho to linux list api
[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                 if (*max_index == 0 || ccc_index(cp) > *max_index)
336                         rc = cl_page_is_under_lock(env, io, page, max_index);
337                 if (rc == 0) {
338                         cp->cpg_defer_uptodate = 1;
339                         cp->cpg_ra_used = 0;
340                         cl_page_list_add(queue, page);
341                         rc = 1;
342                 } else {
343                         cl_page_discard(env, io, page);
344                         rc = -ENOLCK;
345                 }
346         } else {
347                 /* skip completed pages */
348                 cl_page_unassume(env, io, page);
349         }
350         lu_ref_del(&page->cp_reference, "ra", current);
351         cl_page_put(env, page);
352         RETURN(rc);
353 }
354
355 /**
356  * Initiates read-ahead of a page with given index.
357  *
358  * \retval     +ve: page was added to \a queue.
359  *
360  * \retval -ENOLCK: there is no extent lock for this part of a file, stop
361  *                  read-ahead.
362  *
363  * \retval  -ve, 0: page wasn't added to \a queue for other reason.
364  */
365 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
366                               struct cl_page_list *queue,
367                               pgoff_t index, pgoff_t *max_index)
368 {
369         struct cl_object *clob  = io->ci_obj;
370         struct inode     *inode = ccc_object_inode(clob);
371         struct page      *vmpage;
372         struct cl_page   *page;
373         enum ra_stat      which = _NR_RA_STAT; /* keep gcc happy */
374         unsigned int      gfp_mask;
375         int               rc    = 0;
376         const char       *msg   = NULL;
377
378         ENTRY;
379
380         gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
381 #ifdef __GFP_NOWARN
382         gfp_mask |= __GFP_NOWARN;
383 #endif
384         vmpage = grab_cache_page_nowait(inode->i_mapping, index);
385         if (vmpage != NULL) {
386                 /* Check if vmpage was truncated or reclaimed */
387                 if (vmpage->mapping == inode->i_mapping) {
388                         page = cl_page_find(env, clob, vmpage->index,
389                                             vmpage, CPT_CACHEABLE);
390                         if (!IS_ERR(page)) {
391                                 rc = cl_read_ahead_page(env, io, queue,
392                                                         page, clob, max_index);
393                                 if (rc == -ENOLCK) {
394                                         which = RA_STAT_FAILED_MATCH;
395                                         msg   = "lock match failed";
396                                 }
397                         } else {
398                                 which = RA_STAT_FAILED_GRAB_PAGE;
399                                 msg   = "cl_page_find failed";
400                         }
401                 } else {
402                         which = RA_STAT_WRONG_GRAB_PAGE;
403                         msg   = "g_c_p_n returned invalid page";
404                 }
405                 if (rc != 1)
406                         unlock_page(vmpage);
407                 page_cache_release(vmpage);
408         } else {
409                 which = RA_STAT_FAILED_GRAB_PAGE;
410                 msg   = "g_c_p_n failed";
411         }
412         if (msg != NULL) {
413                 ll_ra_stats_inc(inode, which);
414                 CDEBUG(D_READA, "%s\n", msg);
415         }
416         RETURN(rc);
417 }
418
419 #define RIA_DEBUG(ria)                                                       \
420         CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n",       \
421         ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
422         ria->ria_pages)
423
424 /* Limit this to the blocksize instead of PTLRPC_BRW_MAX_SIZE, since we don't
425  * know what the actual RPC size is.  If this needs to change, it makes more
426  * sense to tune the i_blkbits value for the file based on the OSTs it is
427  * striped over, rather than having a constant value for all files here. */
428
429 /* RAS_INCREASE_STEP should be (1UL << (inode->i_blkbits - PAGE_CACHE_SHIFT)).
430  * Temprarily set RAS_INCREASE_STEP to 1MB. After 4MB RPC is enabled
431  * by default, this should be adjusted corresponding with max_read_ahead_mb
432  * and max_read_ahead_per_file_mb otherwise the readahead budget can be used
433  * up quickly which will affect read performance siginificantly. See LU-2816 */
434 #define RAS_INCREASE_STEP(inode) (ONE_MB_BRW_SIZE >> PAGE_CACHE_SHIFT)
435
436 static inline int stride_io_mode(struct ll_readahead_state *ras)
437 {
438         return ras->ras_consecutive_stride_requests > 1;
439 }
440 /* The function calculates how much pages will be read in
441  * [off, off + length], in such stride IO area,
442  * stride_offset = st_off, stride_lengh = st_len,
443  * stride_pages = st_pgs
444  *
445  *   |------------------|*****|------------------|*****|------------|*****|....
446  * st_off
447  *   |--- st_pgs     ---|
448  *   |-----     st_len   -----|
449  *
450  *              How many pages it should read in such pattern
451  *              |-------------------------------------------------------------|
452  *              off
453  *              |<------                  length                      ------->|
454  *
455  *          =   |<----->|  +  |-------------------------------------| +   |---|
456  *             start_left                 st_pgs * i                    end_left
457  */
458 static unsigned long
459 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
460                 unsigned long off, unsigned long length)
461 {
462         __u64 start = off > st_off ? off - st_off : 0;
463         __u64 end = off + length > st_off ? off + length - st_off : 0;
464         unsigned long start_left = 0;
465         unsigned long end_left = 0;
466         unsigned long pg_count;
467
468         if (st_len == 0 || length == 0 || end == 0)
469                 return length;
470
471         start_left = do_div(start, st_len);
472         if (start_left < st_pgs)
473                 start_left = st_pgs - start_left;
474         else
475                 start_left = 0;
476
477         end_left = do_div(end, st_len);
478         if (end_left > st_pgs)
479                 end_left = st_pgs;
480
481         CDEBUG(D_READA, "start "LPU64", end "LPU64" start_left %lu end_left %lu \n",
482                start, end, start_left, end_left);
483
484         if (start == end)
485                 pg_count = end_left - (st_pgs - start_left);
486         else
487                 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
488
489         CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
490                "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
491
492         return pg_count;
493 }
494
495 static int ria_page_count(struct ra_io_arg *ria)
496 {
497         __u64 length = ria->ria_end >= ria->ria_start ?
498                        ria->ria_end - ria->ria_start + 1 : 0;
499
500         return stride_pg_count(ria->ria_stoff, ria->ria_length,
501                                ria->ria_pages, ria->ria_start,
502                                length);
503 }
504
505 /*Check whether the index is in the defined ra-window */
506 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
507 {
508         /* If ria_length == ria_pages, it means non-stride I/O mode,
509          * idx should always inside read-ahead window in this case
510          * For stride I/O mode, just check whether the idx is inside
511          * the ria_pages. */
512         return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
513                (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
514                 ria->ria_length < ria->ria_pages);
515 }
516
517 static int ll_read_ahead_pages(const struct lu_env *env,
518                                struct cl_io *io, struct cl_page_list *queue,
519                                struct ra_io_arg *ria,
520                                unsigned long *reserved_pages,
521                                unsigned long *ra_end)
522 {
523         int rc, count = 0;
524         bool stride_ria;
525         pgoff_t page_idx;
526         pgoff_t max_index = 0;
527
528         LASSERT(ria != NULL);
529         RIA_DEBUG(ria);
530
531         stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
532         for (page_idx = ria->ria_start;
533              page_idx <= ria->ria_end && *reserved_pages > 0; page_idx++) {
534                 if (ras_inside_ra_window(page_idx, ria)) {
535                         /* If the page is inside the read-ahead window*/
536                         rc = ll_read_ahead_page(env, io, queue,
537                                                 page_idx, &max_index);
538                         if (rc == 1) {
539                                 (*reserved_pages)--;
540                                 count++;
541                         } else if (rc == -ENOLCK)
542                                 break;
543                 } else if (stride_ria) {
544                         /* If it is not in the read-ahead window, and it is
545                          * read-ahead mode, then check whether it should skip
546                          * the stride gap */
547                         pgoff_t offset;
548                         /* FIXME: This assertion only is valid when it is for
549                          * forward read-ahead, it will be fixed when backward
550                          * read-ahead is implemented */
551                         LASSERTF(page_idx > ria->ria_stoff, "Invalid page_idx %lu"
552                                 "rs %lu re %lu ro %lu rl %lu rp %lu\n", page_idx,
553                                 ria->ria_start, ria->ria_end, ria->ria_stoff,
554                                 ria->ria_length, ria->ria_pages);
555                         offset = page_idx - ria->ria_stoff;
556                         offset = offset % (ria->ria_length);
557                         if (offset > ria->ria_pages) {
558                                 page_idx += ria->ria_length - offset;
559                                 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
560                                        ria->ria_length - offset);
561                                 continue;
562                         }
563                 }
564         }
565         *ra_end = page_idx;
566         return count;
567 }
568
569 int ll_readahead(const struct lu_env *env, struct cl_io *io,
570                  struct cl_page_list *queue, struct ll_readahead_state *ras,
571                  bool hit)
572 {
573         struct vvp_io *vio = vvp_env_io(env);
574         struct vvp_thread_info *vti = vvp_env_info(env);
575         struct cl_attr *attr = ccc_env_thread_attr(env);
576         unsigned long start = 0, end = 0, reserved;
577         unsigned long ra_end, len, mlen = 0;
578         struct inode *inode;
579         struct ll_ra_read *bead;
580         struct ra_io_arg *ria = &vti->vti_ria;
581         struct cl_object *clob;
582         int ret = 0;
583         __u64 kms;
584         ENTRY;
585
586         clob = io->ci_obj;
587         inode = ccc_object_inode(clob);
588
589         memset(ria, 0, sizeof *ria);
590
591         cl_object_attr_lock(clob);
592         ret = cl_object_attr_get(env, clob, attr);
593         cl_object_attr_unlock(clob);
594
595         if (ret != 0)
596                 RETURN(ret);
597         kms = attr->cat_kms;
598         if (kms == 0) {
599                 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
600                 RETURN(0);
601         }
602
603         spin_lock(&ras->ras_lock);
604         if (vio->cui_ra_window_set)
605                 bead = &vio->cui_bead;
606         else
607                 bead = NULL;
608
609         /* Enlarge the RA window to encompass the full read */
610         if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
611             bead->lrr_start + bead->lrr_count) {
612                 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
613                                       ras->ras_window_start;
614         }
615         /* Reserve a part of the read-ahead window that we'll be issuing */
616         if (ras->ras_window_len) {
617                 start = ras->ras_next_readahead;
618                 end = ras->ras_window_start + ras->ras_window_len - 1;
619         }
620         if (end != 0) {
621                 unsigned long rpc_boundary;
622                 /*
623                  * Align RA window to an optimal boundary.
624                  *
625                  * XXX This would be better to align to cl_max_pages_per_rpc
626                  * instead of PTLRPC_MAX_BRW_PAGES, because the RPC size may
627                  * be aligned to the RAID stripe size in the future and that
628                  * is more important than the RPC size.
629                  */
630                 /* Note: we only trim the RPC, instead of extending the RPC
631                  * to the boundary, so to avoid reading too much pages during
632                  * random reading. */
633                 rpc_boundary = ((end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1)));
634                 if (rpc_boundary > 0)
635                         rpc_boundary--;
636
637                 if (rpc_boundary  > start)
638                         end = rpc_boundary;
639
640                 /* Truncate RA window to end of file */
641                 end = min(end, (unsigned long)((kms - 1) >> PAGE_CACHE_SHIFT));
642
643                 ras->ras_next_readahead = max(end, end + 1);
644                 RAS_CDEBUG(ras);
645         }
646         ria->ria_start = start;
647         ria->ria_end = end;
648         /* If stride I/O mode is detected, get stride window*/
649         if (stride_io_mode(ras)) {
650                 ria->ria_stoff = ras->ras_stride_offset;
651                 ria->ria_length = ras->ras_stride_length;
652                 ria->ria_pages = ras->ras_stride_pages;
653         }
654         spin_unlock(&ras->ras_lock);
655
656         if (end == 0) {
657                 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
658                 RETURN(0);
659         }
660         len = ria_page_count(ria);
661         if (len == 0) {
662                 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
663                 RETURN(0);
664         }
665
666         CDEBUG(D_READA, DFID": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
667                PFID(lu_object_fid(&clob->co_lu)),
668                ria->ria_start, ria->ria_end,
669                bead == NULL ? 0 : bead->lrr_start,
670                bead == NULL ? 0 : bead->lrr_count,
671                hit);
672
673         /* at least to extend the readahead window to cover current read */
674         if (!hit && bead != NULL &&
675             bead->lrr_start + bead->lrr_count > ria->ria_start) {
676                 /* to the end of current read window. */
677                 mlen = bead->lrr_start + bead->lrr_count - ria->ria_start;
678                 /* trim to RPC boundary */
679                 start = ria->ria_start & (PTLRPC_MAX_BRW_PAGES - 1);
680                 mlen = min(mlen, PTLRPC_MAX_BRW_PAGES - start);
681         }
682
683         reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len, mlen);
684         if (reserved < len)
685                 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
686
687         CDEBUG(D_READA, "reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
688                reserved, len, mlen,
689                atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
690                ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
691
692         ret = ll_read_ahead_pages(env, io, queue, ria, &reserved, &ra_end);
693
694         if (reserved != 0)
695                 ll_ra_count_put(ll_i2sbi(inode), reserved);
696
697         if (ra_end == end + 1 && ra_end == (kms >> PAGE_CACHE_SHIFT))
698                 ll_ra_stats_inc(inode, RA_STAT_EOF);
699
700         /* if we didn't get to the end of the region we reserved from
701          * the ras we need to go back and update the ras so that the
702          * next read-ahead tries from where we left off.  we only do so
703          * if the region we failed to issue read-ahead on is still ahead
704          * of the app and behind the next index to start read-ahead from */
705         CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
706                ra_end, end, ria->ria_end);
707
708         if (ra_end != end + 1) {
709                 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
710                 spin_lock(&ras->ras_lock);
711                 if (ra_end < ras->ras_next_readahead &&
712                     index_in_window(ra_end, ras->ras_window_start, 0,
713                                     ras->ras_window_len)) {
714                         ras->ras_next_readahead = ra_end;
715                         RAS_CDEBUG(ras);
716                 }
717                 spin_unlock(&ras->ras_lock);
718         }
719
720         RETURN(ret);
721 }
722
723 static void ras_set_start(struct inode *inode, struct ll_readahead_state *ras,
724                           unsigned long index)
725 {
726         ras->ras_window_start = index & (~(RAS_INCREASE_STEP(inode) - 1));
727 }
728
729 /* called with the ras_lock held or from places where it doesn't matter */
730 static void ras_reset(struct inode *inode, struct ll_readahead_state *ras,
731                       unsigned long index)
732 {
733         ras->ras_last_readpage = index;
734         ras->ras_consecutive_requests = 0;
735         ras->ras_consecutive_pages = 0;
736         ras->ras_window_len = 0;
737         ras_set_start(inode, ras, index);
738         ras->ras_next_readahead = max(ras->ras_window_start, index);
739
740         RAS_CDEBUG(ras);
741 }
742
743 /* called with the ras_lock held or from places where it doesn't matter */
744 static void ras_stride_reset(struct ll_readahead_state *ras)
745 {
746         ras->ras_consecutive_stride_requests = 0;
747         ras->ras_stride_length = 0;
748         ras->ras_stride_pages = 0;
749         RAS_CDEBUG(ras);
750 }
751
752 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
753 {
754         spin_lock_init(&ras->ras_lock);
755         ras_reset(inode, ras, 0);
756         ras->ras_requests = 0;
757         INIT_LIST_HEAD(&ras->ras_read_beads);
758 }
759
760 /*
761  * Check whether the read request is in the stride window.
762  * If it is in the stride window, return 1, otherwise return 0.
763  */
764 static int index_in_stride_window(struct ll_readahead_state *ras,
765                                   unsigned long index)
766 {
767         unsigned long stride_gap;
768
769         if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
770             ras->ras_stride_pages == ras->ras_stride_length)
771                 return 0;
772
773         stride_gap = index - ras->ras_last_readpage - 1;
774
775         /* If it is contiguous read */
776         if (stride_gap == 0)
777                 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
778
779         /* Otherwise check the stride by itself */
780         return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
781                 ras->ras_consecutive_pages == ras->ras_stride_pages;
782 }
783
784 static void ras_update_stride_detector(struct ll_readahead_state *ras,
785                                        unsigned long index)
786 {
787         unsigned long stride_gap = index - ras->ras_last_readpage - 1;
788
789         if (!stride_io_mode(ras) && (stride_gap != 0 ||
790              ras->ras_consecutive_stride_requests == 0)) {
791                 ras->ras_stride_pages = ras->ras_consecutive_pages;
792                 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
793         }
794         LASSERT(ras->ras_request_index == 0);
795         LASSERT(ras->ras_consecutive_stride_requests == 0);
796
797         if (index <= ras->ras_last_readpage) {
798                 /*Reset stride window for forward read*/
799                 ras_stride_reset(ras);
800                 return;
801         }
802
803         ras->ras_stride_pages = ras->ras_consecutive_pages;
804         ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
805
806         RAS_CDEBUG(ras);
807         return;
808 }
809
810 static unsigned long
811 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
812 {
813         return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
814                                ras->ras_stride_pages, ras->ras_stride_offset,
815                                len);
816 }
817
818 /* Stride Read-ahead window will be increased inc_len according to
819  * stride I/O pattern */
820 static void ras_stride_increase_window(struct ll_readahead_state *ras,
821                                        struct ll_ra_info *ra,
822                                        unsigned long inc_len)
823 {
824         unsigned long left, step, window_len;
825         unsigned long stride_len;
826
827         LASSERT(ras->ras_stride_length > 0);
828         LASSERTF(ras->ras_window_start + ras->ras_window_len
829                  >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
830                  " stride_offset %lu\n", ras->ras_window_start,
831                  ras->ras_window_len, ras->ras_stride_offset);
832
833         stride_len = ras->ras_window_start + ras->ras_window_len -
834                      ras->ras_stride_offset;
835
836         left = stride_len % ras->ras_stride_length;
837         window_len = ras->ras_window_len - left;
838
839         if (left < ras->ras_stride_pages)
840                 left += inc_len;
841         else
842                 left = ras->ras_stride_pages + inc_len;
843
844         LASSERT(ras->ras_stride_pages != 0);
845
846         step = left / ras->ras_stride_pages;
847         left %= ras->ras_stride_pages;
848
849         window_len += step * ras->ras_stride_length + left;
850
851         if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
852                 ras->ras_window_len = window_len;
853
854         RAS_CDEBUG(ras);
855 }
856
857 static void ras_increase_window(struct inode *inode,
858                                 struct ll_readahead_state *ras,
859                                 struct ll_ra_info *ra)
860 {
861         /* The stretch of ra-window should be aligned with max rpc_size
862          * but current clio architecture does not support retrieve such
863          * information from lower layer. FIXME later
864          */
865         if (stride_io_mode(ras))
866                 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP(inode));
867         else
868                 ras->ras_window_len = min(ras->ras_window_len +
869                                           RAS_INCREASE_STEP(inode),
870                                           ra->ra_max_pages_per_file);
871 }
872
873 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
874                 struct ll_readahead_state *ras, unsigned long index,
875                 unsigned hit)
876 {
877         struct ll_ra_info *ra = &sbi->ll_ra_info;
878         int zero = 0, stride_detect = 0, ra_miss = 0;
879         ENTRY;
880
881         spin_lock(&ras->ras_lock);
882
883         ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
884
885         /* reset the read-ahead window in two cases.  First when the app seeks
886          * or reads to some other part of the file.  Secondly if we get a
887          * read-ahead miss that we think we've previously issued.  This can
888          * be a symptom of there being so many read-ahead pages that the VM is
889          * reclaiming it before we get to it. */
890         if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
891                 zero = 1;
892                 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
893         } else if (!hit && ras->ras_window_len &&
894                    index < ras->ras_next_readahead &&
895                    index_in_window(index, ras->ras_window_start, 0,
896                                    ras->ras_window_len)) {
897                 ra_miss = 1;
898                 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
899         }
900
901         /* On the second access to a file smaller than the tunable
902          * ra_max_read_ahead_whole_pages trigger RA on all pages in the
903          * file up to ra_max_pages_per_file.  This is simply a best effort
904          * and only occurs once per open file.  Normal RA behavior is reverted
905          * to for subsequent IO.  The mmap case does not increment
906          * ras_requests and thus can never trigger this behavior. */
907         if (ras->ras_requests == 2 && !ras->ras_request_index) {
908                 __u64 kms_pages;
909
910                 kms_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
911                             PAGE_CACHE_SHIFT;
912
913                 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
914                        ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
915
916                 if (kms_pages &&
917                     kms_pages <= ra->ra_max_read_ahead_whole_pages) {
918                         ras->ras_window_start = 0;
919                         ras->ras_last_readpage = 0;
920                         ras->ras_next_readahead = 0;
921                         ras->ras_window_len = min(ra->ra_max_pages_per_file,
922                                 ra->ra_max_read_ahead_whole_pages);
923                         GOTO(out_unlock, 0);
924                 }
925         }
926         if (zero) {
927                 /* check whether it is in stride I/O mode*/
928                 if (!index_in_stride_window(ras, index)) {
929                         if (ras->ras_consecutive_stride_requests == 0 &&
930                             ras->ras_request_index == 0) {
931                                 ras_update_stride_detector(ras, index);
932                                 ras->ras_consecutive_stride_requests++;
933                         } else {
934                                 ras_stride_reset(ras);
935                         }
936                         ras_reset(inode, ras, index);
937                         ras->ras_consecutive_pages++;
938                         GOTO(out_unlock, 0);
939                 } else {
940                         ras->ras_consecutive_pages = 0;
941                         ras->ras_consecutive_requests = 0;
942                         if (++ras->ras_consecutive_stride_requests > 1)
943                                 stride_detect = 1;
944                         RAS_CDEBUG(ras);
945                 }
946         } else {
947                 if (ra_miss) {
948                         if (index_in_stride_window(ras, index) &&
949                             stride_io_mode(ras)) {
950                                 /*If stride-RA hit cache miss, the stride dector
951                                  *will not be reset to avoid the overhead of
952                                  *redetecting read-ahead mode */
953                                 if (index != ras->ras_last_readpage + 1)
954                                         ras->ras_consecutive_pages = 0;
955                                 ras_reset(inode, ras, index);
956                                 RAS_CDEBUG(ras);
957                         } else {
958                                 /* Reset both stride window and normal RA
959                                  * window */
960                                 ras_reset(inode, ras, index);
961                                 ras->ras_consecutive_pages++;
962                                 ras_stride_reset(ras);
963                                 GOTO(out_unlock, 0);
964                         }
965                 } else if (stride_io_mode(ras)) {
966                         /* If this is contiguous read but in stride I/O mode
967                          * currently, check whether stride step still is valid,
968                          * if invalid, it will reset the stride ra window*/
969                         if (!index_in_stride_window(ras, index)) {
970                                 /* Shrink stride read-ahead window to be zero */
971                                 ras_stride_reset(ras);
972                                 ras->ras_window_len = 0;
973                                 ras->ras_next_readahead = index;
974                         }
975                 }
976         }
977         ras->ras_consecutive_pages++;
978         ras->ras_last_readpage = index;
979         ras_set_start(inode, ras, index);
980
981         if (stride_io_mode(ras)) {
982                 /* Since stride readahead is sentivite to the offset
983                  * of read-ahead, so we use original offset here,
984                  * instead of ras_window_start, which is RPC aligned */
985                 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
986         } else {
987                 if (ras->ras_next_readahead < ras->ras_window_start)
988                         ras->ras_next_readahead = ras->ras_window_start;
989                 if (!hit)
990                         ras->ras_next_readahead = index + 1;
991         }
992         RAS_CDEBUG(ras);
993
994         /* Trigger RA in the mmap case where ras_consecutive_requests
995          * is not incremented and thus can't be used to trigger RA */
996         if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
997                 ras->ras_window_len = RAS_INCREASE_STEP(inode);
998                 GOTO(out_unlock, 0);
999         }
1000
1001         /* Initially reset the stride window offset to next_readahead*/
1002         if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
1003                 /**
1004                  * Once stride IO mode is detected, next_readahead should be
1005                  * reset to make sure next_readahead > stride offset
1006                  */
1007                 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
1008                 ras->ras_stride_offset = index;
1009                 ras->ras_window_len = RAS_INCREASE_STEP(inode);
1010         }
1011
1012         /* The initial ras_window_len is set to the request size.  To avoid
1013          * uselessly reading and discarding pages for random IO the window is
1014          * only increased once per consecutive request received. */
1015         if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
1016             !ras->ras_request_index)
1017                 ras_increase_window(inode, ras, ra);
1018         EXIT;
1019 out_unlock:
1020         RAS_CDEBUG(ras);
1021         ras->ras_request_index++;
1022         spin_unlock(&ras->ras_lock);
1023         return;
1024 }
1025
1026 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1027 {
1028         struct inode           *inode = vmpage->mapping->host;
1029         struct ll_inode_info   *lli   = ll_i2info(inode);
1030         struct lu_env          *env;
1031         struct cl_io           *io;
1032         struct cl_page         *page;
1033         struct cl_object       *clob;
1034         struct cl_env_nest      nest;
1035         bool redirtied = false;
1036         bool unlocked = false;
1037         int result;
1038         ENTRY;
1039
1040         LASSERT(PageLocked(vmpage));
1041         LASSERT(!PageWriteback(vmpage));
1042
1043         LASSERT(ll_i2dtexp(inode) != NULL);
1044
1045         env = cl_env_nested_get(&nest);
1046         if (IS_ERR(env))
1047                 GOTO(out, result = PTR_ERR(env));
1048
1049         clob  = ll_i2info(inode)->lli_clob;
1050         LASSERT(clob != NULL);
1051
1052         io = ccc_env_thread_io(env);
1053         io->ci_obj = clob;
1054         io->ci_ignore_layout = 1;
1055         result = cl_io_init(env, io, CIT_MISC, clob);
1056         if (result == 0) {
1057                 page = cl_page_find(env, clob, vmpage->index,
1058                                     vmpage, CPT_CACHEABLE);
1059                 if (!IS_ERR(page)) {
1060                         lu_ref_add(&page->cp_reference, "writepage",
1061                                    current);
1062                         cl_page_assume(env, io, page);
1063                         result = cl_page_flush(env, io, page);
1064                         if (result != 0) {
1065                                 /*
1066                                  * Re-dirty page on error so it retries write,
1067                                  * but not in case when IO has actually
1068                                  * occurred and completed with an error.
1069                                  */
1070                                 if (!PageError(vmpage)) {
1071                                         redirty_page_for_writepage(wbc, vmpage);
1072                                         result = 0;
1073                                         redirtied = true;
1074                                 }
1075                         }
1076                         cl_page_disown(env, io, page);
1077                         unlocked = true;
1078                         lu_ref_del(&page->cp_reference,
1079                                    "writepage", current);
1080                         cl_page_put(env, page);
1081                 } else {
1082                         result = PTR_ERR(page);
1083                 }
1084         }
1085         cl_io_fini(env, io);
1086
1087         if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1088                 loff_t offset = cl_offset(clob, vmpage->index);
1089
1090                 /* Flush page failed because the extent is being written out.
1091                  * Wait for the write of extent to be finished to avoid
1092                  * breaking kernel which assumes ->writepage should mark
1093                  * PageWriteback or clean the page. */
1094                 result = cl_sync_file_range(inode, offset,
1095                                             offset + PAGE_CACHE_SIZE - 1,
1096                                             CL_FSYNC_LOCAL, 1);
1097                 if (result > 0) {
1098                         /* actually we may have written more than one page.
1099                          * decreasing this page because the caller will count
1100                          * it. */
1101                         wbc->nr_to_write -= result - 1;
1102                         result = 0;
1103                 }
1104         }
1105
1106         cl_env_nested_put(&nest, env);
1107         GOTO(out, result);
1108
1109 out:
1110         if (result < 0) {
1111                 if (!lli->lli_async_rc)
1112                         lli->lli_async_rc = result;
1113                 SetPageError(vmpage);
1114                 if (!unlocked)
1115                         unlock_page(vmpage);
1116         }
1117         return result;
1118 }
1119
1120 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1121 {
1122         struct inode *inode = mapping->host;
1123         struct ll_sb_info *sbi = ll_i2sbi(inode);
1124         loff_t start;
1125         loff_t end;
1126         enum cl_fsync_mode mode;
1127         int range_whole = 0;
1128         int result;
1129         int ignore_layout = 0;
1130         ENTRY;
1131
1132         if (wbc->range_cyclic) {
1133                 start = mapping->writeback_index << PAGE_CACHE_SHIFT;
1134                 end = OBD_OBJECT_EOF;
1135         } else {
1136                 start = wbc->range_start;
1137                 end = wbc->range_end;
1138                 if (end == LLONG_MAX) {
1139                         end = OBD_OBJECT_EOF;
1140                         range_whole = start == 0;
1141                 }
1142         }
1143
1144         mode = CL_FSYNC_NONE;
1145         if (wbc->sync_mode == WB_SYNC_ALL)
1146                 mode = CL_FSYNC_LOCAL;
1147
1148         if (sbi->ll_umounting)
1149                 /* if the mountpoint is being umounted, all pages have to be
1150                  * evicted to avoid hitting LBUG when truncate_inode_pages()
1151                  * is called later on. */
1152                 ignore_layout = 1;
1153
1154         if (cl_i2info(inode)->lli_clob == NULL)
1155                 RETURN(0);
1156
1157         result = cl_sync_file_range(inode, start, end, mode, ignore_layout);
1158         if (result > 0) {
1159                 wbc->nr_to_write -= result;
1160                 result = 0;
1161          }
1162
1163         if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1164                 if (end == OBD_OBJECT_EOF)
1165                         mapping->writeback_index = 0;
1166                 else
1167                         mapping->writeback_index = (end >> PAGE_CACHE_SHIFT) +1;
1168         }
1169         RETURN(result);
1170 }
1171
1172 int ll_readpage(struct file *file, struct page *vmpage)
1173 {
1174         struct ll_cl_context *lcc;
1175         int result;
1176         ENTRY;
1177
1178         lcc = ll_cl_init(file, vmpage);
1179         if (!IS_ERR(lcc)) {
1180                 struct lu_env  *env  = lcc->lcc_env;
1181                 struct cl_io   *io   = lcc->lcc_io;
1182                 struct cl_page *page = lcc->lcc_page;
1183
1184                 LASSERT(page->cp_type == CPT_CACHEABLE);
1185                 if (likely(!PageUptodate(vmpage))) {
1186                         cl_page_assume(env, io, page);
1187                         result = cl_io_read_page(env, io, page);
1188                 } else {
1189                         /* Page from a non-object file. */
1190                         unlock_page(vmpage);
1191                         result = 0;
1192                 }
1193                 ll_cl_fini(lcc);
1194         } else {
1195                 unlock_page(vmpage);
1196                 result = PTR_ERR(lcc);
1197         }
1198         RETURN(result);
1199 }
1200
1201 int ll_page_sync_io(const struct lu_env *env, struct cl_io *io,
1202                     struct cl_page *page, enum cl_req_type crt)
1203 {
1204         struct cl_2queue  *queue;
1205         int result;
1206
1207         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
1208
1209         queue = &io->ci_queue;
1210         cl_2queue_init_page(queue, page);
1211
1212         result = cl_io_submit_sync(env, io, crt, queue, 0);
1213         LASSERT(cl_page_is_owned(page, io));
1214
1215         if (crt == CRT_READ)
1216                 /*
1217                  * in CRT_WRITE case page is left locked even in case of
1218                  * error.
1219                  */
1220                 cl_page_list_disown(env, io, &queue->c2_qin);
1221         cl_2queue_fini(env, queue);
1222
1223         return result;
1224 }