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