4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Lustre Lite I/O page cache routines shared by different kernel revs
37 #include <linux/kernel.h>
39 #include <linux/string.h>
40 #include <linux/stat.h>
41 #include <linux/errno.h>
42 #include <linux/unistd.h>
43 #include <linux/writeback.h>
44 #include <linux/uaccess.h>
47 #include <linux/pagemap.h>
48 /* current_is_kswapd() */
49 #include <linux/swap.h>
50 #include <linux/bvec.h>
52 #define DEBUG_SUBSYSTEM S_LLITE
54 #include <obd_cksum.h>
55 #include "llite_internal.h"
57 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
60 * Get readahead pages from the filesystem readahead pool of the client for a
63 * /param sbi superblock for filesystem readahead state ll_ra_info
64 * /param ria per-thread readahead state
65 * /param pages number of pages requested for readahead for the thread.
67 * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
68 * It should work well if the ra_max_pages is much greater than the single
69 * file's read-ahead window, and not too many threads contending for
70 * these readahead pages.
72 * TODO: There may be a 'global sync problem' if many threads are trying
73 * to get an ra budget that is larger than the remaining readahead pages
74 * and reach here at exactly the same time. They will compute /a ret to
75 * consume the remaining pages, but will fail at atomic_add_return() and
76 * get a zero ra window, although there is still ra space remaining. - Jay
78 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
79 struct ra_io_arg *ria,
80 unsigned long pages, unsigned long min)
82 struct ll_ra_info *ra = &sbi->ll_ra_info;
85 /* If read-ahead pages left are less than 1M, do not do read-ahead,
86 * otherwise it will form small read RPC(< 1M), which hurt server
89 ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages), pages);
90 if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages)) {
95 if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
96 atomic_sub(ret, &ra->ra_cur_pages);
102 /* override ra limit for maximum performance */
103 atomic_add(min - ret, &ra->ra_cur_pages);
109 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
111 struct ll_ra_info *ra = &sbi->ll_ra_info;
113 atomic_sub(len, &ra->ra_cur_pages);
116 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
118 LASSERTF(which < _NR_RA_STAT, "which: %u\n", which);
119 lprocfs_counter_incr(sbi->ll_ra_stats, which);
122 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
124 struct ll_sb_info *sbi = ll_i2sbi(inode);
126 ll_ra_stats_inc_sbi(sbi, which);
129 #define RAS_CDEBUG(ras) \
131 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu rpc %lu " \
132 "r %lu ri %lu csr %lu sf %lu sp %lu sl %lu\n", \
133 ras->ras_last_readpage, ras->ras_consecutive_requests, \
134 ras->ras_consecutive_pages, ras->ras_window_start, \
135 ras->ras_window_len, ras->ras_next_readahead, \
137 ras->ras_requests, ras->ras_request_index, \
138 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
139 ras->ras_stride_pages, ras->ras_stride_length)
141 static int index_in_window(unsigned long index, unsigned long point,
142 unsigned long before, unsigned long after)
144 unsigned long start = point - before, end = point + after;
151 return start <= index && index <= end;
154 void ll_ras_enter(struct file *f)
156 struct ll_file_data *fd = LUSTRE_FPRIVATE(f);
157 struct ll_readahead_state *ras = &fd->fd_ras;
159 spin_lock(&ras->ras_lock);
161 ras->ras_request_index = 0;
162 ras->ras_consecutive_requests++;
163 spin_unlock(&ras->ras_lock);
167 * Initiates read-ahead of a page with given index.
169 * \retval +ve: page was already uptodate so it will be skipped
171 * \retval -ve: page wasn't added to \a queue for error;
172 * \retval 0: page was added into \a queue for read ahead.
174 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
175 struct cl_page_list *queue, pgoff_t index)
177 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
178 struct cl_object *clob = io->ci_obj;
179 struct inode *inode = vvp_object_inode(clob);
180 const char *msg = NULL;
181 struct cl_page *page;
182 struct vvp_page *vpg;
186 vmpage = grab_cache_page_nowait(inode->i_mapping, index);
188 which = RA_STAT_FAILED_GRAB_PAGE;
189 msg = "g_c_p_n failed";
194 /* Check if vmpage was truncated or reclaimed */
195 if (vmpage->mapping != inode->i_mapping) {
196 which = RA_STAT_WRONG_GRAB_PAGE;
197 msg = "g_c_p_n returned invalid page";
202 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
204 which = RA_STAT_FAILED_GRAB_PAGE;
205 msg = "cl_page_find failed";
210 lu_ref_add(&page->cp_reference, "ra", current);
211 cl_page_assume(env, io, page);
212 vpg = cl2vvp_page(cl_object_page_slice(clob, page));
213 if (!vpg->vpg_defer_uptodate && !PageUptodate(vmpage)) {
214 vpg->vpg_defer_uptodate = 1;
215 vpg->vpg_ra_used = 0;
216 cl_page_list_add(queue, page);
218 /* skip completed pages */
219 cl_page_unassume(env, io, page);
220 /* This page is already uptodate, returning a positive number
221 * to tell the callers about this
226 lu_ref_del(&page->cp_reference, "ra", current);
227 cl_page_put(env, page);
235 ll_ra_stats_inc(inode, which);
236 CDEBUG(D_READA, "%s\n", msg);
241 #define RIA_DEBUG(ria) \
242 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
243 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
246 static inline int stride_io_mode(struct ll_readahead_state *ras)
248 return ras->ras_consecutive_stride_requests > 1;
251 /* The function calculates how much pages will be read in
252 * [off, off + length], in such stride IO area,
253 * stride_offset = st_off, stride_length = st_len,
254 * stride_pages = st_pgs
256 * |------------------|*****|------------------|*****|------------|*****|....
259 * |----- st_len -----|
261 * How many pages it should read in such pattern
262 * |-------------------------------------------------------------|
264 * |<------ length ------->|
266 * = |<----->| + |-------------------------------------| + |---|
267 * start_left st_pgs * i end_left
270 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
271 unsigned long off, unsigned long length)
273 __u64 start = off > st_off ? off - st_off : 0;
274 __u64 end = off + length > st_off ? off + length - st_off : 0;
275 unsigned long start_left = 0;
276 unsigned long end_left = 0;
277 unsigned long pg_count;
279 if (st_len == 0 || length == 0 || end == 0)
282 start_left = do_div(start, st_len);
283 if (start_left < st_pgs)
284 start_left = st_pgs - start_left;
288 end_left = do_div(end, st_len);
289 if (end_left > st_pgs)
292 CDEBUG(D_READA, "start %llu, end %llu start_left %lu end_left %lu\n",
293 start, end, start_left, end_left);
296 pg_count = end_left - (st_pgs - start_left);
298 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
300 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu pgcount %lu\n",
301 st_off, st_len, st_pgs, off, length, pg_count);
306 static int ria_page_count(struct ra_io_arg *ria)
308 __u64 length = ria->ria_end >= ria->ria_start ?
309 ria->ria_end - ria->ria_start + 1 : 0;
311 return stride_pg_count(ria->ria_stoff, ria->ria_length,
312 ria->ria_pages, ria->ria_start,
316 static unsigned long ras_align(struct ll_readahead_state *ras,
318 unsigned long *remainder)
320 unsigned long rem = index % ras->ras_rpc_size;
327 /*Check whether the index is in the defined ra-window */
328 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
330 /* If ria_length == ria_pages, it means non-stride I/O mode,
331 * idx should always inside read-ahead window in this case
332 * For stride I/O mode, just check whether the idx is inside
335 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
336 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
337 ria->ria_length < ria->ria_pages);
341 ll_read_ahead_pages(const struct lu_env *env, struct cl_io *io,
342 struct cl_page_list *queue, struct ll_readahead_state *ras,
343 struct ra_io_arg *ria)
345 struct cl_read_ahead ra = { 0 };
346 unsigned long ra_end = 0;
354 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
355 for (page_idx = ria->ria_start;
356 page_idx <= ria->ria_end && ria->ria_reserved > 0; page_idx++) {
357 if (ras_inside_ra_window(page_idx, ria)) {
358 if (!ra.cra_end || ra.cra_end < page_idx) {
361 cl_read_ahead_release(env, &ra);
363 rc = cl_io_read_ahead(env, io, page_idx, &ra);
367 CDEBUG(D_READA, "idx: %lu, ra: %lu, rpc: %lu\n",
368 page_idx, ra.cra_end, ra.cra_rpc_size);
369 LASSERTF(ra.cra_end >= page_idx,
370 "object: %p, indcies %lu / %lu\n",
371 io->ci_obj, ra.cra_end, page_idx);
373 * update read ahead RPC size.
374 * NB: it's racy but doesn't matter
376 if (ras->ras_rpc_size > ra.cra_rpc_size &&
378 ras->ras_rpc_size = ra.cra_rpc_size;
379 /* trim it to align with optimal RPC size */
380 end = ras_align(ras, ria->ria_end + 1, NULL);
381 if (end > 0 && !ria->ria_eof)
382 ria->ria_end = end - 1;
383 if (ria->ria_end < ria->ria_end_min)
384 ria->ria_end = ria->ria_end_min;
385 if (ria->ria_end > ra.cra_end)
386 ria->ria_end = ra.cra_end;
389 /* If the page is inside the read-ahead window */
390 rc = ll_read_ahead_page(env, io, queue, page_idx);
397 } else if (stride_ria) {
398 /* If it is not in the read-ahead window, and it is
399 * read-ahead mode, then check whether it should skip
403 /* FIXME: This assertion only is valid when it is for
404 * forward read-ahead, it will be fixed when backward
405 * read-ahead is implemented
407 LASSERTF(page_idx >= ria->ria_stoff, "Invalid page_idx %lu rs %lu re %lu ro %lu rl %lu rp %lu\n",
409 ria->ria_start, ria->ria_end, ria->ria_stoff,
410 ria->ria_length, ria->ria_pages);
411 offset = page_idx - ria->ria_stoff;
412 offset = offset % (ria->ria_length);
413 if (offset > ria->ria_pages) {
414 page_idx += ria->ria_length - offset;
415 CDEBUG(D_READA, "i %lu skip %lu\n", page_idx,
416 ria->ria_length - offset);
421 cl_read_ahead_release(env, &ra);
426 static int ll_readahead(const struct lu_env *env, struct cl_io *io,
427 struct cl_page_list *queue,
428 struct ll_readahead_state *ras, bool hit)
430 struct vvp_io *vio = vvp_env_io(env);
431 struct ll_thread_info *lti = ll_env_info(env);
432 struct cl_attr *attr = vvp_env_thread_attr(env);
433 unsigned long len, mlen = 0;
434 pgoff_t ra_end, start = 0, end = 0;
436 struct ra_io_arg *ria = <i->lti_ria;
437 struct cl_object *clob;
442 inode = vvp_object_inode(clob);
444 memset(ria, 0, sizeof(*ria));
446 cl_object_attr_lock(clob);
447 ret = cl_object_attr_get(env, clob, attr);
448 cl_object_attr_unlock(clob);
454 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
458 spin_lock(&ras->ras_lock);
461 * Note: other thread might rollback the ras_next_readahead,
462 * if it can not get the full size of prepared pages, see the
463 * end of this function. For stride read ahead, it needs to
464 * make sure the offset is no less than ras_stride_offset,
465 * so that stride read ahead can work correctly.
467 if (stride_io_mode(ras))
468 start = max(ras->ras_next_readahead, ras->ras_stride_offset);
470 start = ras->ras_next_readahead;
472 if (ras->ras_window_len > 0)
473 end = ras->ras_window_start + ras->ras_window_len - 1;
475 /* Enlarge the RA window to encompass the full read */
476 if (vio->vui_ra_valid &&
477 end < vio->vui_ra_start + vio->vui_ra_count - 1)
478 end = vio->vui_ra_start + vio->vui_ra_count - 1;
481 unsigned long end_index;
483 /* Truncate RA window to end of file */
484 end_index = (unsigned long)((kms - 1) >> PAGE_SHIFT);
485 if (end_index <= end) {
490 ras->ras_next_readahead = max(end, end + 1);
493 ria->ria_start = start;
495 /* If stride I/O mode is detected, get stride window*/
496 if (stride_io_mode(ras)) {
497 ria->ria_stoff = ras->ras_stride_offset;
498 ria->ria_length = ras->ras_stride_length;
499 ria->ria_pages = ras->ras_stride_pages;
501 spin_unlock(&ras->ras_lock);
504 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
507 len = ria_page_count(ria);
509 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
513 CDEBUG(D_READA, DFID ": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
514 PFID(lu_object_fid(&clob->co_lu)),
515 ria->ria_start, ria->ria_end,
516 vio->vui_ra_valid ? vio->vui_ra_start : 0,
517 vio->vui_ra_valid ? vio->vui_ra_count : 0,
520 /* at least to extend the readahead window to cover current read */
521 if (!hit && vio->vui_ra_valid &&
522 vio->vui_ra_start + vio->vui_ra_count > ria->ria_start) {
523 unsigned long remainder;
525 /* to the end of current read window. */
526 mlen = vio->vui_ra_start + vio->vui_ra_count - ria->ria_start;
527 /* trim to RPC boundary */
528 ras_align(ras, ria->ria_start, &remainder);
529 mlen = min(mlen, ras->ras_rpc_size - remainder);
530 ria->ria_end_min = ria->ria_start + mlen;
533 ria->ria_reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len, mlen);
534 if (ria->ria_reserved < len)
535 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
537 CDEBUG(D_READA, "reserved pages %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
538 ria->ria_reserved, len, mlen,
539 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
540 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
542 ra_end = ll_read_ahead_pages(env, io, queue, ras, ria);
544 if (ria->ria_reserved)
545 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
547 if (ra_end == end && ra_end == (kms >> PAGE_SHIFT))
548 ll_ra_stats_inc(inode, RA_STAT_EOF);
550 /* if we didn't get to the end of the region we reserved from
551 * the ras we need to go back and update the ras so that the
552 * next read-ahead tries from where we left off. we only do so
553 * if the region we failed to issue read-ahead on is still ahead
554 * of the app and behind the next index to start read-ahead from
556 CDEBUG(D_READA, "ra_end = %lu end = %lu stride end = %lu pages = %d\n",
557 ra_end, end, ria->ria_end, ret);
559 if (ra_end > 0 && ra_end != end) {
560 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
561 spin_lock(&ras->ras_lock);
562 if (ra_end <= ras->ras_next_readahead &&
563 index_in_window(ra_end, ras->ras_window_start, 0,
564 ras->ras_window_len)) {
565 ras->ras_next_readahead = ra_end + 1;
568 spin_unlock(&ras->ras_lock);
574 static void ras_set_start(struct inode *inode, struct ll_readahead_state *ras,
577 ras->ras_window_start = ras_align(ras, index, NULL);
580 /* called with the ras_lock held or from places where it doesn't matter */
581 static void ras_reset(struct inode *inode, struct ll_readahead_state *ras,
584 ras->ras_last_readpage = index;
585 ras->ras_consecutive_requests = 0;
586 ras->ras_consecutive_pages = 0;
587 ras->ras_window_len = 0;
588 ras_set_start(inode, ras, index);
589 ras->ras_next_readahead = max(ras->ras_window_start, index + 1);
594 /* called with the ras_lock held or from places where it doesn't matter */
595 static void ras_stride_reset(struct ll_readahead_state *ras)
597 ras->ras_consecutive_stride_requests = 0;
598 ras->ras_stride_length = 0;
599 ras->ras_stride_pages = 0;
603 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
605 spin_lock_init(&ras->ras_lock);
606 ras->ras_rpc_size = PTLRPC_MAX_BRW_PAGES;
607 ras_reset(inode, ras, 0);
608 ras->ras_requests = 0;
612 * Check whether the read request is in the stride window.
613 * If it is in the stride window, return 1, otherwise return 0.
615 static int index_in_stride_window(struct ll_readahead_state *ras,
618 unsigned long stride_gap;
620 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
621 ras->ras_stride_pages == ras->ras_stride_length)
624 stride_gap = index - ras->ras_last_readpage - 1;
626 /* If it is contiguous read */
628 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
630 /* Otherwise check the stride by itself */
631 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
632 ras->ras_consecutive_pages == ras->ras_stride_pages;
635 static void ras_update_stride_detector(struct ll_readahead_state *ras,
638 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
640 if ((stride_gap != 0 || ras->ras_consecutive_stride_requests == 0) &&
641 !stride_io_mode(ras)) {
642 ras->ras_stride_pages = ras->ras_consecutive_pages;
643 ras->ras_stride_length = ras->ras_consecutive_pages +
646 LASSERT(ras->ras_request_index == 0);
647 LASSERT(ras->ras_consecutive_stride_requests == 0);
649 if (index <= ras->ras_last_readpage) {
650 /*Reset stride window for forward read*/
651 ras_stride_reset(ras);
655 ras->ras_stride_pages = ras->ras_consecutive_pages;
656 ras->ras_stride_length = stride_gap + ras->ras_consecutive_pages;
661 /* Stride Read-ahead window will be increased inc_len according to
664 static void ras_stride_increase_window(struct ll_readahead_state *ras,
665 struct ll_ra_info *ra,
666 unsigned long inc_len)
668 unsigned long left, step, window_len;
669 unsigned long stride_len;
671 LASSERT(ras->ras_stride_length > 0);
672 LASSERTF(ras->ras_window_start + ras->ras_window_len
673 >= ras->ras_stride_offset, "window_start %lu, window_len %lu stride_offset %lu\n",
674 ras->ras_window_start,
675 ras->ras_window_len, ras->ras_stride_offset);
677 stride_len = ras->ras_window_start + ras->ras_window_len -
678 ras->ras_stride_offset;
680 left = stride_len % ras->ras_stride_length;
681 window_len = ras->ras_window_len - left;
683 if (left < ras->ras_stride_pages)
686 left = ras->ras_stride_pages + inc_len;
688 LASSERT(ras->ras_stride_pages != 0);
690 step = left / ras->ras_stride_pages;
691 left %= ras->ras_stride_pages;
693 window_len += step * ras->ras_stride_length + left;
695 if (stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
696 ras->ras_stride_pages, ras->ras_stride_offset,
697 window_len) <= ra->ra_max_pages_per_file)
698 ras->ras_window_len = window_len;
703 static void ras_increase_window(struct inode *inode,
704 struct ll_readahead_state *ras,
705 struct ll_ra_info *ra)
707 /* The stretch of ra-window should be aligned with max rpc_size
708 * but current clio architecture does not support retrieve such
709 * information from lower layer. FIXME later
711 if (stride_io_mode(ras)) {
712 ras_stride_increase_window(ras, ra, ras->ras_rpc_size);
716 wlen = min(ras->ras_window_len + ras->ras_rpc_size,
717 ra->ra_max_pages_per_file);
718 ras->ras_window_len = ras_align(ras, wlen, NULL);
722 static void ras_update(struct ll_sb_info *sbi, struct inode *inode,
723 struct ll_readahead_state *ras, unsigned long index,
724 enum ras_update_flags flags)
726 struct ll_ra_info *ra = &sbi->ll_ra_info;
727 int zero = 0, stride_detect = 0, ra_miss = 0;
728 bool hit = flags & LL_RAS_HIT;
730 spin_lock(&ras->ras_lock);
733 CDEBUG(D_READA, DFID " pages at %lu miss.\n",
734 PFID(ll_inode2fid(inode)), index);
736 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
738 /* reset the read-ahead window in two cases. First when the app seeks
739 * or reads to some other part of the file. Secondly if we get a
740 * read-ahead miss that we think we've previously issued. This can
741 * be a symptom of there being so many read-ahead pages that the VM is
742 * reclaiming it before we get to it.
744 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
746 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
747 } else if (!hit && ras->ras_window_len &&
748 index < ras->ras_next_readahead &&
749 index_in_window(index, ras->ras_window_start, 0,
750 ras->ras_window_len)) {
752 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
755 /* On the second access to a file smaller than the tunable
756 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
757 * file up to ra_max_pages_per_file. This is simply a best effort
758 * and only occurs once per open file. Normal RA behavior is reverted
759 * to for subsequent IO. The mmap case does not increment
760 * ras_requests and thus can never trigger this behavior.
762 if (ras->ras_requests >= 2 && !ras->ras_request_index) {
765 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
768 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
769 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
772 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
773 ras->ras_window_start = 0;
774 ras->ras_next_readahead = index + 1;
775 ras->ras_window_len = min(ra->ra_max_pages_per_file,
776 ra->ra_max_read_ahead_whole_pages);
781 /* check whether it is in stride I/O mode*/
782 if (!index_in_stride_window(ras, index)) {
783 if (ras->ras_consecutive_stride_requests == 0 &&
784 ras->ras_request_index == 0) {
785 ras_update_stride_detector(ras, index);
786 ras->ras_consecutive_stride_requests++;
788 ras_stride_reset(ras);
790 ras_reset(inode, ras, index);
791 ras->ras_consecutive_pages++;
794 ras->ras_consecutive_pages = 0;
795 ras->ras_consecutive_requests = 0;
796 if (++ras->ras_consecutive_stride_requests > 1)
802 if (index_in_stride_window(ras, index) &&
803 stride_io_mode(ras)) {
804 if (index != ras->ras_last_readpage + 1)
805 ras->ras_consecutive_pages = 0;
806 ras_reset(inode, ras, index);
808 /* If stride-RA hit cache miss, the stride
809 * detector will not be reset to avoid the
810 * overhead of redetecting read-ahead mode,
811 * but on the condition that the stride window
812 * is still intersect with normal sequential
815 if (ras->ras_window_start <
816 ras->ras_stride_offset)
817 ras_stride_reset(ras);
820 /* Reset both stride window and normal RA
823 ras_reset(inode, ras, index);
824 ras->ras_consecutive_pages++;
825 ras_stride_reset(ras);
828 } else if (stride_io_mode(ras)) {
829 /* If this is contiguous read but in stride I/O mode
830 * currently, check whether stride step still is valid,
831 * if invalid, it will reset the stride ra window
833 if (!index_in_stride_window(ras, index)) {
834 /* Shrink stride read-ahead window to be zero */
835 ras_stride_reset(ras);
836 ras->ras_window_len = 0;
837 ras->ras_next_readahead = index;
841 ras->ras_consecutive_pages++;
842 ras->ras_last_readpage = index;
843 ras_set_start(inode, ras, index);
845 if (stride_io_mode(ras)) {
846 /* Since stride readahead is sensitive to the offset
847 * of read-ahead, so we use original offset here,
848 * instead of ras_window_start, which is RPC aligned
850 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
851 ras->ras_window_start = max(ras->ras_stride_offset,
852 ras->ras_window_start);
854 if (ras->ras_next_readahead < ras->ras_window_start)
855 ras->ras_next_readahead = ras->ras_window_start;
857 ras->ras_next_readahead = index + 1;
861 /* Trigger RA in the mmap case where ras_consecutive_requests
862 * is not incremented and thus can't be used to trigger RA
864 if (ras->ras_consecutive_pages >= 4 && flags & LL_RAS_MMAP) {
865 ras_increase_window(inode, ras, ra);
867 * reset consecutive pages so that the readahead window can
870 ras->ras_consecutive_pages = 0;
874 /* Initially reset the stride window offset to next_readahead*/
875 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
877 * Once stride IO mode is detected, next_readahead should be
878 * reset to make sure next_readahead > stride offset
880 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
881 ras->ras_stride_offset = index;
882 ras->ras_window_start = max(index, ras->ras_window_start);
885 /* The initial ras_window_len is set to the request size. To avoid
886 * uselessly reading and discarding pages for random IO the window is
887 * only increased once per consecutive request received. */
888 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
889 !ras->ras_request_index)
890 ras_increase_window(inode, ras, ra);
893 ras->ras_request_index++;
894 spin_unlock(&ras->ras_lock);
897 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
899 struct inode *inode = vmpage->mapping->host;
900 struct ll_inode_info *lli = ll_i2info(inode);
903 struct cl_page *page;
904 struct cl_object *clob;
905 bool redirtied = false;
906 bool unlocked = false;
910 LASSERT(PageLocked(vmpage));
911 LASSERT(!PageWriteback(vmpage));
913 LASSERT(ll_i2dtexp(inode));
915 env = cl_env_get(&refcheck);
917 result = PTR_ERR(env);
921 clob = ll_i2info(inode)->lli_clob;
924 io = vvp_env_thread_io(env);
926 io->ci_ignore_layout = 1;
927 result = cl_io_init(env, io, CIT_MISC, clob);
929 page = cl_page_find(env, clob, vmpage->index,
930 vmpage, CPT_CACHEABLE);
932 lu_ref_add(&page->cp_reference, "writepage",
934 cl_page_assume(env, io, page);
935 result = cl_page_flush(env, io, page);
938 * Re-dirty page on error so it retries write,
939 * but not in case when IO has actually
940 * occurred and completed with an error.
942 if (!PageError(vmpage)) {
943 redirty_page_for_writepage(wbc, vmpage);
948 cl_page_disown(env, io, page);
950 lu_ref_del(&page->cp_reference,
951 "writepage", current);
952 cl_page_put(env, page);
954 result = PTR_ERR(page);
959 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
960 loff_t offset = cl_offset(clob, vmpage->index);
962 /* Flush page failed because the extent is being written out.
963 * Wait for the write of extent to be finished to avoid
964 * breaking kernel which assumes ->writepage should mark
965 * PageWriteback or clean the page.
967 result = cl_sync_file_range(inode, offset,
968 offset + PAGE_SIZE - 1,
971 /* actually we may have written more than one page.
972 * decreasing this page because the caller will count
975 wbc->nr_to_write -= result - 1;
980 cl_env_put(env, &refcheck);
985 if (!lli->lli_async_rc)
986 lli->lli_async_rc = result;
987 SetPageError(vmpage);
994 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
996 struct inode *inode = mapping->host;
997 struct ll_sb_info *sbi = ll_i2sbi(inode);
1000 enum cl_fsync_mode mode;
1001 int range_whole = 0;
1003 int ignore_layout = 0;
1005 if (wbc->range_cyclic) {
1006 start = mapping->writeback_index << PAGE_SHIFT;
1007 end = OBD_OBJECT_EOF;
1009 start = wbc->range_start;
1010 end = wbc->range_end;
1011 if (end == LLONG_MAX) {
1012 end = OBD_OBJECT_EOF;
1013 range_whole = start == 0;
1017 mode = CL_FSYNC_NONE;
1018 if (wbc->sync_mode == WB_SYNC_ALL)
1019 mode = CL_FSYNC_LOCAL;
1021 if (sbi->ll_umounting)
1022 /* if the mountpoint is being umounted, all pages have to be
1023 * evicted to avoid hitting LBUG when truncate_inode_pages()
1024 * is called later on.
1028 if (!ll_i2info(inode)->lli_clob)
1031 result = cl_sync_file_range(inode, start, end, mode, ignore_layout);
1033 wbc->nr_to_write -= result;
1037 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1038 if (end == OBD_OBJECT_EOF)
1039 mapping->writeback_index = 0;
1041 mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
1046 struct ll_cl_context *ll_cl_find(struct file *file)
1048 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1049 struct ll_cl_context *lcc;
1050 struct ll_cl_context *found = NULL;
1052 read_lock(&fd->fd_lock);
1053 list_for_each_entry(lcc, &fd->fd_lccs, lcc_list) {
1054 if (lcc->lcc_cookie == current) {
1059 read_unlock(&fd->fd_lock);
1064 void ll_cl_add(struct file *file, const struct lu_env *env, struct cl_io *io)
1066 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1067 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1069 memset(lcc, 0, sizeof(*lcc));
1070 INIT_LIST_HEAD(&lcc->lcc_list);
1071 lcc->lcc_cookie = current;
1075 write_lock(&fd->fd_lock);
1076 list_add(&lcc->lcc_list, &fd->fd_lccs);
1077 write_unlock(&fd->fd_lock);
1080 void ll_cl_remove(struct file *file, const struct lu_env *env)
1082 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1083 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1085 write_lock(&fd->fd_lock);
1086 list_del_init(&lcc->lcc_list);
1087 write_unlock(&fd->fd_lock);
1090 static int ll_io_read_page(const struct lu_env *env, struct cl_io *io,
1091 struct cl_page *page)
1093 struct inode *inode = vvp_object_inode(page->cp_obj);
1094 struct ll_file_data *fd = vvp_env_io(env)->vui_fd;
1095 struct ll_readahead_state *ras = &fd->fd_ras;
1096 struct cl_2queue *queue = &io->ci_queue;
1097 struct ll_sb_info *sbi = ll_i2sbi(inode);
1098 struct vvp_page *vpg;
1102 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1103 uptodate = vpg->vpg_defer_uptodate;
1105 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1106 sbi->ll_ra_info.ra_max_pages > 0) {
1107 struct vvp_io *vio = vvp_env_io(env);
1108 enum ras_update_flags flags = 0;
1111 flags |= LL_RAS_HIT;
1112 if (!vio->vui_ra_valid)
1113 flags |= LL_RAS_MMAP;
1114 ras_update(sbi, inode, ras, vvp_index(vpg), flags);
1117 cl_2queue_init(queue);
1119 vpg->vpg_ra_used = 1;
1120 cl_page_export(env, page, 1);
1121 cl_page_disown(env, io, page);
1123 cl_page_list_add(&queue->c2_qin, page);
1126 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1127 sbi->ll_ra_info.ra_max_pages > 0) {
1130 rc2 = ll_readahead(env, io, &queue->c2_qin, ras,
1132 CDEBUG(D_READA, DFID "%d pages read ahead at %lu\n",
1133 PFID(ll_inode2fid(inode)), rc2, vvp_index(vpg));
1136 if (queue->c2_qin.pl_nr > 0)
1137 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
1140 * Unlock unsent pages in case of error.
1142 cl_page_list_disown(env, io, &queue->c2_qin);
1143 cl_2queue_fini(env, queue);
1148 int ll_readpage(struct file *file, struct page *vmpage)
1150 struct cl_object *clob = ll_i2info(file_inode(file))->lli_clob;
1151 struct ll_cl_context *lcc;
1152 const struct lu_env *env;
1154 struct cl_page *page;
1157 lcc = ll_cl_find(file);
1159 unlock_page(vmpage);
1165 LASSERT(io->ci_state == CIS_IO_GOING);
1166 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
1167 if (!IS_ERR(page)) {
1168 LASSERT(page->cp_type == CPT_CACHEABLE);
1169 if (likely(!PageUptodate(vmpage))) {
1170 cl_page_assume(env, io, page);
1171 result = ll_io_read_page(env, io, page);
1173 /* Page from a non-object file. */
1174 unlock_page(vmpage);
1177 cl_page_put(env, page);
1179 unlock_page(vmpage);
1180 result = PTR_ERR(page);
1185 int ll_page_sync_io(const struct lu_env *env, struct cl_io *io,
1186 struct cl_page *page, enum cl_req_type crt)
1188 struct cl_2queue *queue;
1191 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
1193 queue = &io->ci_queue;
1194 cl_2queue_init_page(queue, page);
1196 result = cl_io_submit_sync(env, io, crt, queue, 0);
1197 LASSERT(cl_page_is_owned(page, io));
1199 if (crt == CRT_READ)
1201 * in CRT_WRITE case page is left locked even in case of
1204 cl_page_list_disown(env, io, &queue->c2_qin);
1205 cl_2queue_fini(env, queue);