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, 2016, 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 <asm/uaccess.h>
47 #include <linux/stat.h>
48 #include <asm/uaccess.h>
50 #include <linux/pagemap.h>
51 /* current_is_kswapd() */
52 #include <linux/swap.h>
54 #define DEBUG_SUBSYSTEM S_LLITE
56 #include <obd_cksum.h>
57 #include "llite_internal.h"
58 #include <lustre_compat.h>
60 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
63 * Get readahead pages from the filesystem readahead pool of the client for a
66 * /param sbi superblock for filesystem readahead state ll_ra_info
67 * /param ria per-thread readahead state
68 * /param pages number of pages requested for readahead for the thread.
70 * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
71 * It should work well if the ra_max_pages is much greater than the single
72 * file's read-ahead window, and not too many threads contending for
73 * these readahead pages.
75 * TODO: There may be a 'global sync problem' if many threads are trying
76 * to get an ra budget that is larger than the remaining readahead pages
77 * and reach here at exactly the same time. They will compute /a ret to
78 * consume the remaining pages, but will fail at atomic_add_return() and
79 * get a zero ra window, although there is still ra space remaining. - Jay */
81 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
82 struct ra_io_arg *ria,
83 unsigned long pages, unsigned long min)
85 struct ll_ra_info *ra = &sbi->ll_ra_info;
89 /* If read-ahead pages left are less than 1M, do not do read-ahead,
90 * otherwise it will form small read RPC(< 1M), which hurt server
91 * performance a lot. */
92 ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages),
94 if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages))
97 if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
98 atomic_sub(ret, &ra->ra_cur_pages);
104 /* override ra limit for maximum performance */
105 atomic_add(min - ret, &ra->ra_cur_pages);
111 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
113 struct ll_ra_info *ra = &sbi->ll_ra_info;
114 atomic_sub(len, &ra->ra_cur_pages);
117 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
119 LASSERTF(which < _NR_RA_STAT, "which: %u\n", which);
120 lprocfs_counter_incr(sbi->ll_ra_stats, which);
123 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
125 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 struct cl_object *clob = io->ci_obj;
178 struct inode *inode = vvp_object_inode(clob);
180 struct cl_page *page;
181 struct vvp_page *vpg;
182 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
184 const char *msg = NULL;
187 vmpage = grab_cache_page_nowait(inode->i_mapping, index);
188 if (vmpage == NULL) {
189 which = RA_STAT_FAILED_GRAB_PAGE;
190 msg = "g_c_p_n failed";
191 GOTO(out, rc = -EBUSY);
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";
198 GOTO(out, rc = -EBUSY);
201 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
203 which = RA_STAT_FAILED_GRAB_PAGE;
204 msg = "cl_page_find failed";
205 GOTO(out, rc = PTR_ERR(page));
208 lu_ref_add(&page->cp_reference, "ra", current);
209 cl_page_assume(env, io, page);
210 vpg = cl2vvp_page(cl_object_page_slice(clob, page));
211 if (!vpg->vpg_defer_uptodate && !PageUptodate(vmpage)) {
212 vpg->vpg_defer_uptodate = 1;
213 vpg->vpg_ra_used = 0;
214 cl_page_list_add(queue, page);
216 /* skip completed pages */
217 cl_page_unassume(env, io, page);
218 /* This page is already uptodate, returning a positive number
219 * to tell the callers about this */
223 lu_ref_del(&page->cp_reference, "ra", current);
224 cl_page_put(env, page);
227 if (vmpage != NULL) {
233 ll_ra_stats_inc(inode, which);
234 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_lengh = 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"
301 "pgcount %lu\n", 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;
321 if (remainder != NULL)
326 /*Check whether the index is in the defined ra-window */
327 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
329 /* If ria_length == ria_pages, it means non-stride I/O mode,
330 * idx should always inside read-ahead window in this case
331 * For stride I/O mode, just check whether the idx is inside
333 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
334 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
335 ria->ria_length < ria->ria_pages);
339 ll_read_ahead_pages(const struct lu_env *env, struct cl_io *io,
340 struct cl_page_list *queue, struct ll_readahead_state *ras,
341 struct ra_io_arg *ria, pgoff_t *ra_end)
343 struct cl_read_ahead ra = { 0 };
344 int rc = 0, count = 0;
348 LASSERT(ria != NULL);
351 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
352 for (page_idx = ria->ria_start;
353 page_idx <= ria->ria_end && ria->ria_reserved > 0; page_idx++) {
354 if (ras_inside_ra_window(page_idx, ria)) {
355 if (ra.cra_end == 0 || ra.cra_end < page_idx) {
358 cl_read_ahead_release(env, &ra);
360 rc = cl_io_read_ahead(env, io, page_idx, &ra);
364 CDEBUG(D_READA, "idx: %lu, ra: %lu, rpc: %lu\n",
365 page_idx, ra.cra_end, ra.cra_rpc_size);
366 LASSERTF(ra.cra_end >= page_idx,
367 "object: %p, indcies %lu / %lu\n",
368 io->ci_obj, ra.cra_end, page_idx);
369 /* update read ahead RPC size.
370 * NB: it's racy but doesn't matter */
371 if (ras->ras_rpc_size > ra.cra_rpc_size &&
373 ras->ras_rpc_size = ra.cra_rpc_size;
374 /* trim it to align with optimal RPC size */
375 end = ras_align(ras, ria->ria_end + 1, NULL);
376 if (end > 0 && !ria->ria_eof)
377 ria->ria_end = end - 1;
378 if (ria->ria_end < ria->ria_end_min)
379 ria->ria_end = ria->ria_end_min;
380 if (ria->ria_end > ra.cra_end)
381 ria->ria_end = ra.cra_end;
383 if (page_idx > ria->ria_end)
386 /* If the page is inside the read-ahead window */
387 rc = ll_read_ahead_page(env, io, queue, page_idx);
392 /* Only subtract from reserve & count the page if we
393 * really did readahead on that page. */
398 } else if (stride_ria) {
399 /* If it is not in the read-ahead window, and it is
400 * 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 */
406 LASSERTF(page_idx >= ria->ria_stoff,
407 "Invalid page_idx %lu rs %lu re %lu ro %lu "
408 "rl %lu rp %lu\n", page_idx,
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);
422 cl_read_ahead_release(env, &ra);
427 static int ll_readahead(const struct lu_env *env, struct cl_io *io,
428 struct cl_page_list *queue,
429 struct ll_readahead_state *ras, bool hit)
431 struct vvp_io *vio = vvp_env_io(env);
432 struct ll_thread_info *lti = ll_env_info(env);
433 struct cl_attr *attr = vvp_env_thread_attr(env);
434 unsigned long len, mlen = 0;
435 pgoff_t ra_end = 0, start = 0, end = 0;
437 struct ra_io_arg *ria = <i->lti_ria;
438 struct cl_object *clob;
444 inode = vvp_object_inode(clob);
446 memset(ria, 0, sizeof *ria);
448 cl_object_attr_lock(clob);
449 ret = cl_object_attr_get(env, clob, attr);
450 cl_object_attr_unlock(clob);
456 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
460 spin_lock(&ras->ras_lock);
463 * Note: other thread might rollback the ras_next_readahead,
464 * if it can not get the full size of prepared pages, see the
465 * end of this function. For stride read ahead, it needs to
466 * make sure the offset is no less than ras_stride_offset,
467 * so that stride read ahead can work correctly.
469 if (stride_io_mode(ras))
470 start = max(ras->ras_next_readahead, ras->ras_stride_offset);
472 start = ras->ras_next_readahead;
474 if (ras->ras_window_len > 0)
475 end = ras->ras_window_start + ras->ras_window_len - 1;
477 /* Enlarge the RA window to encompass the full read */
478 if (vio->vui_ra_valid &&
479 end < vio->vui_ra_start + vio->vui_ra_count - 1)
480 end = vio->vui_ra_start + vio->vui_ra_count - 1;
483 unsigned long end_index;
485 /* Truncate RA window to end of file */
486 end_index = (unsigned long)((kms - 1) >> PAGE_SHIFT);
487 if (end_index <= end) {
492 ras->ras_next_readahead = max(end, end + 1);
495 ria->ria_start = start;
497 /* If stride I/O mode is detected, get stride window*/
498 if (stride_io_mode(ras)) {
499 ria->ria_stoff = ras->ras_stride_offset;
500 ria->ria_length = ras->ras_stride_length;
501 ria->ria_pages = ras->ras_stride_pages;
503 spin_unlock(&ras->ras_lock);
506 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
509 len = ria_page_count(ria);
511 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
515 CDEBUG(D_READA, DFID": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
516 PFID(lu_object_fid(&clob->co_lu)),
517 ria->ria_start, ria->ria_end,
518 vio->vui_ra_valid ? vio->vui_ra_start : 0,
519 vio->vui_ra_valid ? vio->vui_ra_count : 0,
522 /* at least to extend the readahead window to cover current read */
523 if (!hit && vio->vui_ra_valid &&
524 vio->vui_ra_start + vio->vui_ra_count > ria->ria_start) {
525 unsigned long remainder;
527 /* to the end of current read window. */
528 mlen = vio->vui_ra_start + vio->vui_ra_count - ria->ria_start;
529 /* trim to RPC boundary */
530 ras_align(ras, ria->ria_start, &remainder);
531 mlen = min(mlen, ras->ras_rpc_size - remainder);
532 ria->ria_end_min = ria->ria_start + mlen;
535 ria->ria_reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len, mlen);
536 if (ria->ria_reserved < len)
537 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
539 CDEBUG(D_READA, "reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
540 ria->ria_reserved, len, mlen,
541 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
542 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
544 ret = ll_read_ahead_pages(env, io, queue, ras, ria, &ra_end);
546 if (ria->ria_reserved != 0)
547 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
549 if (ra_end == end && ra_end == (kms >> PAGE_SHIFT))
550 ll_ra_stats_inc(inode, RA_STAT_EOF);
552 /* if we didn't get to the end of the region we reserved from
553 * the ras we need to go back and update the ras so that the
554 * next read-ahead tries from where we left off. we only do so
555 * if the region we failed to issue read-ahead on is still ahead
556 * of the app and behind the next index to start read-ahead from */
557 CDEBUG(D_READA, "ra_end = %lu end = %lu stride end = %lu pages = %d\n",
558 ra_end, end, ria->ria_end, ret);
560 if (ra_end > 0 && ra_end != end) {
561 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
562 spin_lock(&ras->ras_lock);
563 if (ra_end <= ras->ras_next_readahead &&
564 index_in_window(ra_end, ras->ras_window_start, 0,
565 ras->ras_window_len)) {
566 ras->ras_next_readahead = ra_end + 1;
569 spin_unlock(&ras->ras_lock);
575 static void ras_set_start(struct inode *inode, struct ll_readahead_state *ras,
578 ras->ras_window_start = ras_align(ras, index, NULL);
581 /* called with the ras_lock held or from places where it doesn't matter */
582 static void ras_reset(struct inode *inode, struct ll_readahead_state *ras,
585 ras->ras_last_readpage = index;
586 ras->ras_consecutive_requests = 0;
587 ras->ras_consecutive_pages = 0;
588 ras->ras_window_len = 0;
589 ras_set_start(inode, ras, index);
590 ras->ras_next_readahead = max(ras->ras_window_start, index + 1);
595 /* called with the ras_lock held or from places where it doesn't matter */
596 static void ras_stride_reset(struct ll_readahead_state *ras)
598 ras->ras_consecutive_stride_requests = 0;
599 ras->ras_stride_length = 0;
600 ras->ras_stride_pages = 0;
604 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
606 spin_lock_init(&ras->ras_lock);
607 ras->ras_rpc_size = PTLRPC_MAX_BRW_PAGES;
608 ras_reset(inode, ras, 0);
609 ras->ras_requests = 0;
613 * Check whether the read request is in the stride window.
614 * If it is in the stride window, return 1, otherwise return 0.
616 static int index_in_stride_window(struct ll_readahead_state *ras,
619 unsigned long stride_gap;
621 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
622 ras->ras_stride_pages == ras->ras_stride_length)
625 stride_gap = index - ras->ras_last_readpage - 1;
627 /* If it is contiguous read */
629 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
631 /* Otherwise check the stride by itself */
632 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
633 ras->ras_consecutive_pages == ras->ras_stride_pages;
636 static void ras_update_stride_detector(struct ll_readahead_state *ras,
639 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
641 if (!stride_io_mode(ras) && (stride_gap != 0 ||
642 ras->ras_consecutive_stride_requests == 0)) {
643 ras->ras_stride_pages = ras->ras_consecutive_pages;
644 ras->ras_stride_length = stride_gap +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;
663 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
665 return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
666 ras->ras_stride_pages, ras->ras_stride_offset,
670 /* Stride Read-ahead window will be increased inc_len according to
671 * stride I/O pattern */
672 static void ras_stride_increase_window(struct ll_readahead_state *ras,
673 struct ll_ra_info *ra,
674 unsigned long inc_len)
676 unsigned long left, step, window_len;
677 unsigned long stride_len;
679 LASSERT(ras->ras_stride_length > 0);
680 LASSERTF(ras->ras_window_start + ras->ras_window_len
681 >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
682 " stride_offset %lu\n", ras->ras_window_start,
683 ras->ras_window_len, ras->ras_stride_offset);
685 stride_len = ras->ras_window_start + ras->ras_window_len -
686 ras->ras_stride_offset;
688 left = stride_len % ras->ras_stride_length;
689 window_len = ras->ras_window_len - left;
691 if (left < ras->ras_stride_pages)
694 left = ras->ras_stride_pages + inc_len;
696 LASSERT(ras->ras_stride_pages != 0);
698 step = left / ras->ras_stride_pages;
699 left %= ras->ras_stride_pages;
701 window_len += step * ras->ras_stride_length + left;
703 if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
704 ras->ras_window_len = window_len;
709 static void ras_increase_window(struct inode *inode,
710 struct ll_readahead_state *ras,
711 struct ll_ra_info *ra)
713 /* The stretch of ra-window should be aligned with max rpc_size
714 * but current clio architecture does not support retrieve such
715 * information from lower layer. FIXME later
717 if (stride_io_mode(ras)) {
718 ras_stride_increase_window(ras, ra, ras->ras_rpc_size);
722 wlen = min(ras->ras_window_len + ras->ras_rpc_size,
723 ra->ra_max_pages_per_file);
724 ras->ras_window_len = ras_align(ras, wlen, NULL);
728 static void ras_update(struct ll_sb_info *sbi, struct inode *inode,
729 struct ll_readahead_state *ras, unsigned long index,
730 enum ras_update_flags flags)
732 struct ll_ra_info *ra = &sbi->ll_ra_info;
733 bool hit = flags & LL_RAS_HIT;
734 int zero = 0, stride_detect = 0, ra_miss = 0;
737 spin_lock(&ras->ras_lock);
740 CDEBUG(D_READA, DFID " pages at %lu miss.\n",
741 PFID(ll_inode2fid(inode)), index);
742 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
744 /* reset the read-ahead window in two cases. First when the app seeks
745 * or reads to some other part of the file. Secondly if we get a
746 * read-ahead miss that we think we've previously issued. This can
747 * be a symptom of there being so many read-ahead pages that the VM is
748 * reclaiming it before we get to it. */
749 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
751 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
752 } else if (!hit && ras->ras_window_len &&
753 index < ras->ras_next_readahead &&
754 index_in_window(index, ras->ras_window_start, 0,
755 ras->ras_window_len)) {
757 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
760 /* On the second access to a file smaller than the tunable
761 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
762 * file up to ra_max_pages_per_file. This is simply a best effort
763 * and only occurs once per open file. Normal RA behavior is reverted
764 * to for subsequent IO. The mmap case does not increment
765 * ras_requests and thus can never trigger this behavior. */
766 if (ras->ras_requests >= 2 && !ras->ras_request_index) {
769 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
772 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
773 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
776 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
777 ras->ras_window_start = 0;
778 ras->ras_next_readahead = index + 1;
779 ras->ras_window_len = min(ra->ra_max_pages_per_file,
780 ra->ra_max_read_ahead_whole_pages);
785 /* check whether it is in stride I/O mode*/
786 if (!index_in_stride_window(ras, index)) {
787 if (ras->ras_consecutive_stride_requests == 0 &&
788 ras->ras_request_index == 0) {
789 ras_update_stride_detector(ras, index);
790 ras->ras_consecutive_stride_requests++;
792 ras_stride_reset(ras);
794 ras_reset(inode, ras, index);
795 ras->ras_consecutive_pages++;
798 ras->ras_consecutive_pages = 0;
799 ras->ras_consecutive_requests = 0;
800 if (++ras->ras_consecutive_stride_requests > 1)
806 if (index_in_stride_window(ras, index) &&
807 stride_io_mode(ras)) {
808 if (index != ras->ras_last_readpage + 1)
809 ras->ras_consecutive_pages = 0;
810 ras_reset(inode, ras, index);
812 /* If stride-RA hit cache miss, the stride
813 * detector will not be reset to avoid the
814 * overhead of redetecting read-ahead mode,
815 * but on the condition that the stride window
816 * is still intersect with normal sequential
817 * read-ahead window. */
818 if (ras->ras_window_start <
819 ras->ras_stride_offset)
820 ras_stride_reset(ras);
823 /* Reset both stride window and normal RA
825 ras_reset(inode, ras, index);
826 ras->ras_consecutive_pages++;
827 ras_stride_reset(ras);
830 } else if (stride_io_mode(ras)) {
831 /* If this is contiguous read but in stride I/O mode
832 * currently, check whether stride step still is valid,
833 * if invalid, it will reset the stride ra window*/
834 if (!index_in_stride_window(ras, index)) {
835 /* Shrink stride read-ahead window to be zero */
836 ras_stride_reset(ras);
837 ras->ras_window_len = 0;
838 ras->ras_next_readahead = index;
842 ras->ras_consecutive_pages++;
843 ras->ras_last_readpage = index;
844 ras_set_start(inode, ras, index);
846 if (stride_io_mode(ras)) {
847 /* Since stride readahead is sentivite to the offset
848 * of read-ahead, so we use original offset here,
849 * 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 */
863 if (ras->ras_consecutive_pages >= 4 && flags & LL_RAS_MMAP) {
864 ras_increase_window(inode, ras, ra);
865 /* reset consecutive pages so that the readahead window can
867 ras->ras_consecutive_pages = 0;
871 /* Initially reset the stride window offset to next_readahead*/
872 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
874 * Once stride IO mode is detected, next_readahead should be
875 * reset to make sure next_readahead > stride offset
877 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
878 ras->ras_stride_offset = index;
879 ras->ras_window_start = max(index, ras->ras_window_start);
882 /* The initial ras_window_len is set to the request size. To avoid
883 * uselessly reading and discarding pages for random IO the window is
884 * only increased once per consecutive request received. */
885 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
886 !ras->ras_request_index)
887 ras_increase_window(inode, ras, ra);
891 ras->ras_request_index++;
892 spin_unlock(&ras->ras_lock);
896 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
898 struct inode *inode = vmpage->mapping->host;
899 struct ll_inode_info *lli = ll_i2info(inode);
902 struct cl_page *page;
903 struct cl_object *clob;
904 bool redirtied = false;
905 bool unlocked = false;
910 LASSERT(PageLocked(vmpage));
911 LASSERT(!PageWriteback(vmpage));
913 LASSERT(ll_i2dtexp(inode) != NULL);
915 env = cl_env_get(&refcheck);
917 GOTO(out, result = PTR_ERR(env));
919 clob = ll_i2info(inode)->lli_clob;
920 LASSERT(clob != NULL);
922 io = vvp_env_thread_io(env);
924 io->ci_ignore_layout = 1;
925 result = cl_io_init(env, io, CIT_MISC, clob);
927 page = cl_page_find(env, clob, vmpage->index,
928 vmpage, CPT_CACHEABLE);
930 lu_ref_add(&page->cp_reference, "writepage",
932 cl_page_assume(env, io, page);
933 result = cl_page_flush(env, io, page);
936 * Re-dirty page on error so it retries write,
937 * but not in case when IO has actually
938 * occurred and completed with an error.
940 if (!PageError(vmpage)) {
941 redirty_page_for_writepage(wbc, vmpage);
946 cl_page_disown(env, io, page);
948 lu_ref_del(&page->cp_reference,
949 "writepage", current);
950 cl_page_put(env, page);
952 result = PTR_ERR(page);
957 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
958 loff_t offset = cl_offset(clob, vmpage->index);
960 /* Flush page failed because the extent is being written out.
961 * Wait for the write of extent to be finished to avoid
962 * breaking kernel which assumes ->writepage should mark
963 * PageWriteback or clean the page. */
964 result = cl_sync_file_range(inode, offset,
965 offset + PAGE_SIZE - 1,
968 /* actually we may have written more than one page.
969 * decreasing this page because the caller will count
971 wbc->nr_to_write -= result - 1;
976 cl_env_put(env, &refcheck);
981 if (!lli->lli_async_rc)
982 lli->lli_async_rc = result;
983 SetPageError(vmpage);
990 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
992 struct inode *inode = mapping->host;
995 enum cl_fsync_mode mode;
1000 if (wbc->range_cyclic) {
1001 start = mapping->writeback_index << PAGE_SHIFT;
1002 end = OBD_OBJECT_EOF;
1004 start = wbc->range_start;
1005 end = wbc->range_end;
1006 if (end == LLONG_MAX) {
1007 end = OBD_OBJECT_EOF;
1008 range_whole = start == 0;
1012 mode = CL_FSYNC_NONE;
1013 if (wbc->sync_mode == WB_SYNC_ALL)
1014 mode = CL_FSYNC_LOCAL;
1016 if (ll_i2info(inode)->lli_clob == NULL)
1019 /* for directio, it would call writepages() to evict cached pages
1020 * inside the IO context of write, which will cause deadlock at
1021 * layout_conf since it waits for active IOs to complete. */
1022 result = cl_sync_file_range(inode, start, end, mode, 1);
1024 wbc->nr_to_write -= result;
1028 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1029 if (end == OBD_OBJECT_EOF)
1030 mapping->writeback_index = 0;
1032 mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
1037 struct ll_cl_context *ll_cl_find(struct file *file)
1039 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1040 struct ll_cl_context *lcc;
1041 struct ll_cl_context *found = NULL;
1043 read_lock(&fd->fd_lock);
1044 list_for_each_entry(lcc, &fd->fd_lccs, lcc_list) {
1045 if (lcc->lcc_cookie == current) {
1050 read_unlock(&fd->fd_lock);
1055 void ll_cl_add(struct file *file, const struct lu_env *env, struct cl_io *io,
1058 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1059 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1061 memset(lcc, 0, sizeof(*lcc));
1062 INIT_LIST_HEAD(&lcc->lcc_list);
1063 lcc->lcc_cookie = current;
1066 lcc->lcc_type = type;
1068 write_lock(&fd->fd_lock);
1069 list_add(&lcc->lcc_list, &fd->fd_lccs);
1070 write_unlock(&fd->fd_lock);
1073 void ll_cl_remove(struct file *file, const struct lu_env *env)
1075 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1076 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1078 write_lock(&fd->fd_lock);
1079 list_del_init(&lcc->lcc_list);
1080 write_unlock(&fd->fd_lock);
1083 static int ll_io_read_page(const struct lu_env *env, struct cl_io *io,
1084 struct cl_page *page, struct file *file)
1086 struct inode *inode = vvp_object_inode(page->cp_obj);
1087 struct ll_sb_info *sbi = ll_i2sbi(inode);
1088 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1089 struct ll_readahead_state *ras = &fd->fd_ras;
1090 struct cl_2queue *queue = &io->ci_queue;
1091 struct vvp_page *vpg;
1096 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1097 uptodate = vpg->vpg_defer_uptodate;
1099 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1100 sbi->ll_ra_info.ra_max_pages > 0 &&
1101 !vpg->vpg_ra_updated) {
1102 struct vvp_io *vio = vvp_env_io(env);
1103 enum ras_update_flags flags = 0;
1106 flags |= LL_RAS_HIT;
1107 if (!vio->vui_ra_valid)
1108 flags |= LL_RAS_MMAP;
1109 ras_update(sbi, inode, ras, vvp_index(vpg), flags);
1112 cl_2queue_init(queue);
1114 vpg->vpg_ra_used = 1;
1115 cl_page_export(env, page, 1);
1116 cl_page_disown(env, io, page);
1118 cl_2queue_add(queue, page);
1121 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1122 sbi->ll_ra_info.ra_max_pages > 0) {
1125 rc2 = ll_readahead(env, io, &queue->c2_qin, ras,
1127 CDEBUG(D_READA, DFID "%d pages read ahead at %lu\n",
1128 PFID(ll_inode2fid(inode)), rc2, vvp_index(vpg));
1131 if (queue->c2_qin.pl_nr > 0)
1132 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
1135 * Unlock unsent pages in case of error.
1137 cl_page_list_disown(env, io, &queue->c2_qin);
1138 cl_2queue_fini(env, queue);
1143 int ll_readpage(struct file *file, struct page *vmpage)
1145 struct inode *inode = file_inode(file);
1146 struct cl_object *clob = ll_i2info(inode)->lli_clob;
1147 struct ll_cl_context *lcc;
1148 const struct lu_env *env = NULL;
1149 struct cl_io *io = NULL;
1150 struct cl_page *page;
1154 lcc = ll_cl_find(file);
1160 if (io == NULL) { /* fast read */
1161 struct inode *inode = file_inode(file);
1162 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1163 struct ll_readahead_state *ras = &fd->fd_ras;
1164 struct lu_env *local_env = NULL;
1165 struct vvp_page *vpg;
1169 /* TODO: need to verify the layout version to make sure
1170 * the page is not invalid due to layout change. */
1171 page = cl_vmpage_page(vmpage, clob);
1173 unlock_page(vmpage);
1178 local_env = cl_env_percpu_get();
1182 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1183 if (vpg->vpg_defer_uptodate) {
1184 enum ras_update_flags flags = LL_RAS_HIT;
1186 if (lcc && lcc->lcc_type == LCC_MMAP)
1187 flags |= LL_RAS_MMAP;
1189 /* For fast read, it updates read ahead state only
1190 * if the page is hit in cache because non cache page
1191 * case will be handled by slow read later. */
1192 ras_update(ll_i2sbi(inode), inode, ras, vvp_index(vpg),
1194 /* avoid duplicate ras_update() call */
1195 vpg->vpg_ra_updated = 1;
1197 /* Check if we can issue a readahead RPC, if that is
1198 * the case, we can't do fast IO because we will need
1199 * a cl_io to issue the RPC. */
1200 if (ras->ras_window_start + ras->ras_window_len <
1201 ras->ras_next_readahead + PTLRPC_MAX_BRW_PAGES) {
1202 /* export the page and skip io stack */
1203 vpg->vpg_ra_used = 1;
1204 cl_page_export(env, page, 1);
1209 /* release page refcount before unlocking the page to ensure
1210 * the object won't be destroyed in the calling path of
1211 * cl_page_put(). Please see comment in ll_releasepage(). */
1212 cl_page_put(env, page);
1213 unlock_page(vmpage);
1215 cl_env_percpu_put(local_env);
1220 LASSERT(io->ci_state == CIS_IO_GOING);
1221 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
1222 if (!IS_ERR(page)) {
1223 LASSERT(page->cp_type == CPT_CACHEABLE);
1224 if (likely(!PageUptodate(vmpage))) {
1225 cl_page_assume(env, io, page);
1226 result = ll_io_read_page(env, io, page, file);
1228 /* Page from a non-object file. */
1229 unlock_page(vmpage);
1232 cl_page_put(env, page);
1234 unlock_page(vmpage);
1235 result = PTR_ERR(page);
1240 int ll_page_sync_io(const struct lu_env *env, struct cl_io *io,
1241 struct cl_page *page, enum cl_req_type crt)
1243 struct cl_2queue *queue;
1246 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
1248 queue = &io->ci_queue;
1249 cl_2queue_init_page(queue, page);
1251 result = cl_io_submit_sync(env, io, crt, queue, 0);
1252 LASSERT(cl_page_is_owned(page, io));
1254 if (crt == CRT_READ)
1256 * in CRT_WRITE case page is left locked even in case of
1259 cl_page_list_disown(env, io, &queue->c2_qin);
1260 cl_2queue_fini(env, queue);