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, 2017, 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/file.h>
48 #include <linux/stat.h>
49 #include <asm/uaccess.h>
51 #include <linux/pagemap.h>
52 /* current_is_kswapd() */
53 #include <linux/swap.h>
54 #include <linux/task_io_accounting_ops.h>
56 #define DEBUG_SUBSYSTEM S_LLITE
58 #include <obd_cksum.h>
59 #include "llite_internal.h"
60 #include <lustre_compat.h>
62 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
65 * Get readahead pages from the filesystem readahead pool of the client for a
68 * /param sbi superblock for filesystem readahead state ll_ra_info
69 * /param ria per-thread readahead state
70 * /param pages number of pages requested for readahead for the thread.
72 * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
73 * It should work well if the ra_max_pages is much greater than the single
74 * file's read-ahead window, and not too many threads contending for
75 * these readahead pages.
77 * TODO: There may be a 'global sync problem' if many threads are trying
78 * to get an ra budget that is larger than the remaining readahead pages
79 * and reach here at exactly the same time. They will compute /a ret to
80 * consume the remaining pages, but will fail at atomic_add_return() and
81 * get a zero ra window, although there is still ra space remaining. - Jay */
83 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
84 struct ra_io_arg *ria,
86 unsigned long pages_min)
88 struct ll_ra_info *ra = &sbi->ll_ra_info;
92 /* If read-ahead pages left are less than 1M, do not do read-ahead,
93 * otherwise it will form small read RPC(< 1M), which hurt server
94 * performance a lot. */
95 ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages),
97 if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages))
100 if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
101 atomic_sub(ret, &ra->ra_cur_pages);
106 if (ret < pages_min) {
107 /* override ra limit for maximum performance */
108 atomic_add(pages_min - ret, &ra->ra_cur_pages);
114 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long pages)
116 struct ll_ra_info *ra = &sbi->ll_ra_info;
117 atomic_sub(pages, &ra->ra_cur_pages);
120 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
122 LASSERTF(which < _NR_RA_STAT, "which: %u\n", which);
123 lprocfs_counter_incr(sbi->ll_ra_stats, which);
126 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
128 struct ll_sb_info *sbi = ll_i2sbi(inode);
129 ll_ra_stats_inc_sbi(sbi, which);
132 #define RAS_CDEBUG(ras) \
134 "lre %llu cr %lu cb %llu wsi %lu wp %lu nra %lu rpc %lu " \
135 "r %lu csr %lu so %llu sb %llu sl %llu lr %lu\n", \
136 ras->ras_last_read_end_bytes, ras->ras_consecutive_requests, \
137 ras->ras_consecutive_bytes, ras->ras_window_start_idx, \
138 ras->ras_window_pages, ras->ras_next_readahead_idx, \
139 ras->ras_rpc_pages, ras->ras_requests, \
140 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
141 ras->ras_stride_bytes, ras->ras_stride_length, \
142 ras->ras_async_last_readpage_idx)
144 static bool pos_in_window(loff_t pos, loff_t point,
145 unsigned long before, unsigned long after)
147 loff_t start = point - before;
148 loff_t end = point + after;
155 return start <= pos && pos <= end;
159 * Initiates read-ahead of a page with given index.
161 * \retval +ve: page was already uptodate so it will be skipped
163 * \retval -ve: page wasn't added to \a queue for error;
164 * \retval 0: page was added into \a queue for read ahead.
166 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
167 struct cl_page_list *queue, pgoff_t index)
169 struct cl_object *clob = io->ci_obj;
170 struct inode *inode = vvp_object_inode(clob);
172 struct cl_page *page;
173 struct vvp_page *vpg;
174 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
176 const char *msg = NULL;
179 vmpage = grab_cache_page_nowait(inode->i_mapping, index);
180 if (vmpage == NULL) {
181 which = RA_STAT_FAILED_GRAB_PAGE;
182 msg = "g_c_p_n failed";
183 GOTO(out, rc = -EBUSY);
186 /* Check if vmpage was truncated or reclaimed */
187 if (vmpage->mapping != inode->i_mapping) {
188 which = RA_STAT_WRONG_GRAB_PAGE;
189 msg = "g_c_p_n returned invalid page";
190 GOTO(out, rc = -EBUSY);
193 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
195 which = RA_STAT_FAILED_GRAB_PAGE;
196 msg = "cl_page_find failed";
197 GOTO(out, rc = PTR_ERR(page));
200 lu_ref_add(&page->cp_reference, "ra", current);
201 cl_page_assume(env, io, page);
202 vpg = cl2vvp_page(cl_object_page_slice(clob, page));
203 if (!vpg->vpg_defer_uptodate && !PageUptodate(vmpage)) {
204 vpg->vpg_defer_uptodate = 1;
205 vpg->vpg_ra_used = 0;
206 cl_page_list_add(queue, page);
208 /* skip completed pages */
209 cl_page_unassume(env, io, page);
210 /* This page is already uptodate, returning a positive number
211 * to tell the callers about this */
215 lu_ref_del(&page->cp_reference, "ra", current);
216 cl_page_put(env, page);
219 if (vmpage != NULL) {
225 ll_ra_stats_inc(inode, which);
226 CDEBUG(D_READA, "%s\n", msg);
233 #define RIA_DEBUG(ria) \
234 CDEBUG(D_READA, "rs %lu re %lu ro %llu rl %llu rb %llu\n", \
235 ria->ria_start_idx, ria->ria_end_idx, ria->ria_stoff, \
236 ria->ria_length, ria->ria_bytes)
238 static inline int stride_io_mode(struct ll_readahead_state *ras)
240 return ras->ras_consecutive_stride_requests > 1;
243 /* The function calculates how many bytes will be read in
244 * [off, off + length], in such stride IO area,
245 * stride_offset = st_off, stride_lengh = st_len,
246 * stride_bytes = st_bytes
248 * |------------------|*****|------------------|*****|------------|*****|....
251 * |----- st_len -----|
253 * How many bytes it should read in such pattern
254 * |-------------------------------------------------------------|
256 * |<------ length ------->|
258 * = |<----->| + |-------------------------------------| + |---|
259 * start_left st_bytes * i end_left
261 static loff_t stride_byte_count(loff_t st_off, loff_t st_len, loff_t st_bytes,
262 loff_t off, loff_t length)
264 u64 start = off > st_off ? off - st_off : 0;
265 u64 end = off + length > st_off ? off + length - st_off : 0;
270 if (st_len == 0 || length == 0 || end == 0)
273 start = div64_u64_rem(start, st_len, &start_left);
274 if (start_left < st_bytes)
275 start_left = st_bytes - start_left;
279 end = div64_u64_rem(end, st_len, &end_left);
280 if (end_left > st_bytes)
283 CDEBUG(D_READA, "start %llu, end %llu start_left %llu end_left %llu\n",
284 start, end, start_left, end_left);
287 bytes_count = end_left - (st_bytes - start_left);
289 bytes_count = start_left +
290 st_bytes * (end - start - 1) + end_left;
293 "st_off %llu, st_len %llu st_bytes %llu off %llu length %llu bytescount %llu\n",
294 st_off, st_len, st_bytes, off, length, bytes_count);
299 static unsigned long ria_page_count(struct ra_io_arg *ria)
301 loff_t length_bytes = ria->ria_end_idx >= ria->ria_start_idx ?
302 (loff_t)(ria->ria_end_idx -
303 ria->ria_start_idx + 1) << PAGE_SHIFT : 0;
306 if (ria->ria_length > ria->ria_bytes && ria->ria_bytes &&
307 (ria->ria_length & ~PAGE_MASK || ria->ria_bytes & ~PAGE_MASK ||
308 ria->ria_stoff & ~PAGE_MASK)) {
309 /* Over-estimate un-aligned page stride read */
310 unsigned long pg_count = ((ria->ria_bytes +
311 PAGE_SIZE - 1) >> PAGE_SHIFT) + 1;
312 pg_count *= length_bytes / ria->ria_length + 1;
316 bytes_count = stride_byte_count(ria->ria_stoff, ria->ria_length,
318 (loff_t)ria->ria_start_idx<<PAGE_SHIFT,
320 return (bytes_count + PAGE_SIZE - 1) >> PAGE_SHIFT;
323 static pgoff_t ras_align(struct ll_readahead_state *ras, pgoff_t index)
325 return index - (index % ras->ras_rpc_pages);
328 /* Check whether the index is in the defined ra-window */
329 static bool ras_inside_ra_window(pgoff_t idx, struct ra_io_arg *ria)
331 loff_t pos = (loff_t)idx << PAGE_SHIFT;
333 /* If ria_length == ria_bytes, it means non-stride I/O mode,
334 * idx should always inside read-ahead window in this case
335 * For stride I/O mode, just check whether the idx is inside
338 if (ria->ria_length == 0 || ria->ria_length == ria->ria_bytes)
341 if (pos >= ria->ria_stoff) {
344 div64_u64_rem(pos - ria->ria_stoff, ria->ria_length, &offset);
346 if (offset < ria->ria_bytes ||
347 (ria->ria_length - offset) < PAGE_SIZE)
349 } else if (pos + PAGE_SIZE > ria->ria_stoff) {
357 ll_read_ahead_pages(const struct lu_env *env, struct cl_io *io,
358 struct cl_page_list *queue, struct ll_readahead_state *ras,
359 struct ra_io_arg *ria, pgoff_t *ra_end)
361 struct cl_read_ahead ra = { 0 };
362 /* busy page count is per stride */
363 int rc = 0, count = 0, busy_page_count = 0;
366 LASSERT(ria != NULL);
369 for (page_idx = ria->ria_start_idx;
370 page_idx <= ria->ria_end_idx && ria->ria_reserved > 0;
372 if (ras_inside_ra_window(page_idx, ria)) {
373 if (ra.cra_end_idx == 0 || ra.cra_end_idx < page_idx) {
377 * Do not shrink ria_end_idx at any case until
378 * the minimum end of current read is covered.
380 * Do not extend read lock accross stripe if
381 * lock contention detected.
383 if (ra.cra_contention &&
384 page_idx > ria->ria_end_idx_min) {
385 ria->ria_end_idx = *ra_end;
389 cl_read_ahead_release(env, &ra);
391 rc = cl_io_read_ahead(env, io, page_idx, &ra);
396 * Only shrink ria_end_idx if the matched
397 * LDLM lock doesn't cover more.
399 if (page_idx > ra.cra_end_idx) {
400 ria->ria_end_idx = ra.cra_end_idx;
404 CDEBUG(D_READA, "idx: %lu, ra: %lu, rpc: %lu\n",
405 page_idx, ra.cra_end_idx,
407 LASSERTF(ra.cra_end_idx >= page_idx,
408 "object: %p, indcies %lu / %lu\n",
409 io->ci_obj, ra.cra_end_idx, page_idx);
410 /* update read ahead RPC size.
411 * NB: it's racy but doesn't matter */
412 if (ras->ras_rpc_pages != ra.cra_rpc_pages &&
413 ra.cra_rpc_pages > 0)
414 ras->ras_rpc_pages = ra.cra_rpc_pages;
415 /* trim it to align with optimal RPC size */
416 end_idx = ras_align(ras, ria->ria_end_idx + 1);
417 if (end_idx > 0 && !ria->ria_eof)
418 ria->ria_end_idx = end_idx - 1;
419 if (ria->ria_end_idx < ria->ria_end_idx_min)
420 ria->ria_end_idx = ria->ria_end_idx_min;
422 if (page_idx > ria->ria_end_idx)
425 /* If the page is inside the read-ahead window */
426 rc = ll_read_ahead_page(env, io, queue, page_idx);
427 if (rc < 0 && rc != -EBUSY)
432 "skip busy page: %lu\n", page_idx);
433 /* For page unaligned readahead the first
434 * last pages of each region can be read by
435 * another reader on the same node, and so
436 * may be busy. So only stop for > 2 busy
438 if (busy_page_count > 2)
443 /* Only subtract from reserve & count the page if we
444 * really did readahead on that page. */
449 } else if (stride_io_mode(ras)) {
450 /* If it is not in the read-ahead window, and it is
451 * read-ahead mode, then check whether it should skip
454 loff_t pos = (loff_t)page_idx << PAGE_SHIFT;
457 div64_u64_rem(pos - ria->ria_stoff, ria->ria_length,
459 if (offset >= ria->ria_bytes) {
460 pos += (ria->ria_length - offset);
461 if ((pos >> PAGE_SHIFT) >= page_idx + 1)
462 page_idx = (pos >> PAGE_SHIFT) - 1;
465 "Stride: jump %llu pages to %lu\n",
466 ria->ria_length - offset, page_idx);
472 cl_read_ahead_release(env, &ra);
477 static void ll_readahead_work_free(struct ll_readahead_work *work)
479 fput(work->lrw_file);
483 static void ll_readahead_handle_work(struct work_struct *wq);
484 static void ll_readahead_work_add(struct inode *inode,
485 struct ll_readahead_work *work)
487 INIT_WORK(&work->lrw_readahead_work, ll_readahead_handle_work);
488 queue_work(ll_i2sbi(inode)->ll_ra_info.ll_readahead_wq,
489 &work->lrw_readahead_work);
492 static int ll_readahead_file_kms(const struct lu_env *env,
493 struct cl_io *io, __u64 *kms)
495 struct cl_object *clob;
497 struct cl_attr *attr = vvp_env_thread_attr(env);
501 inode = vvp_object_inode(clob);
503 cl_object_attr_lock(clob);
504 ret = cl_object_attr_get(env, clob, attr);
505 cl_object_attr_unlock(clob);
510 *kms = attr->cat_kms;
514 static void ll_readahead_handle_work(struct work_struct *wq)
516 struct ll_readahead_work *work;
519 struct ra_io_arg *ria;
521 struct ll_file_data *fd;
522 struct ll_readahead_state *ras;
524 struct cl_2queue *queue;
525 pgoff_t ra_end_idx = 0;
526 unsigned long pages, pages_min = 0;
531 struct ll_sb_info *sbi;
533 work = container_of(wq, struct ll_readahead_work,
535 fd = work->lrw_file->private_data;
537 file = work->lrw_file;
538 inode = file_inode(file);
539 sbi = ll_i2sbi(inode);
541 env = cl_env_alloc(&refcheck, LCT_NOREF);
543 GOTO(out_free_work, rc = PTR_ERR(env));
545 io = vvp_env_thread_io(env);
546 ll_io_init(io, file, CIT_READ, NULL);
548 rc = ll_readahead_file_kms(env, io, &kms);
550 GOTO(out_put_env, rc);
553 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
554 GOTO(out_put_env, rc = 0);
557 ria = &ll_env_info(env)->lti_ria;
558 memset(ria, 0, sizeof(*ria));
560 ria->ria_start_idx = work->lrw_start_idx;
561 /* Truncate RA window to end of file */
562 eof_index = (pgoff_t)(kms - 1) >> PAGE_SHIFT;
563 if (eof_index <= work->lrw_end_idx) {
564 work->lrw_end_idx = eof_index;
567 if (work->lrw_end_idx <= work->lrw_start_idx)
568 GOTO(out_put_env, rc = 0);
570 ria->ria_end_idx = work->lrw_end_idx;
571 pages = ria->ria_end_idx - ria->ria_start_idx + 1;
572 ria->ria_reserved = ll_ra_count_get(sbi, ria,
573 ria_page_count(ria), pages_min);
576 "async reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
577 ria->ria_reserved, pages, pages_min,
578 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
579 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
581 if (ria->ria_reserved < pages) {
582 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
583 if (PAGES_TO_MiB(ria->ria_reserved) < 1) {
584 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
585 GOTO(out_put_env, rc = 0);
589 rc = cl_io_rw_init(env, io, CIT_READ, ria->ria_start_idx, pages);
591 GOTO(out_put_env, rc);
593 vvp_env_io(env)->vui_io_subtype = IO_NORMAL;
594 vvp_env_io(env)->vui_fd = fd;
595 io->ci_state = CIS_LOCKED;
596 io->ci_async_readahead = true;
597 rc = cl_io_start(env, io);
599 GOTO(out_io_fini, rc);
601 queue = &io->ci_queue;
602 cl_2queue_init(queue);
604 rc = ll_read_ahead_pages(env, io, &queue->c2_qin, ras, ria,
606 if (ria->ria_reserved != 0)
607 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
608 if (queue->c2_qin.pl_nr > 0) {
609 int count = queue->c2_qin.pl_nr;
611 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
613 task_io_account_read(PAGE_SIZE * count);
615 if (ria->ria_end_idx == ra_end_idx && ra_end_idx == (kms >> PAGE_SHIFT))
616 ll_ra_stats_inc(inode, RA_STAT_EOF);
618 if (ra_end_idx != ria->ria_end_idx)
619 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
621 /* TODO: discard all pages until page reinit route is implemented */
622 cl_page_list_discard(env, io, &queue->c2_qin);
624 /* Unlock unsent read pages in case of error. */
625 cl_page_list_disown(env, io, &queue->c2_qin);
627 cl_2queue_fini(env, queue);
632 cl_env_put(env, &refcheck);
635 ll_ra_stats_inc_sbi(ll_i2sbi(inode), RA_STAT_ASYNC);
636 atomic_dec(&sbi->ll_ra_info.ra_async_inflight);
637 ll_readahead_work_free(work);
640 static int ll_readahead(const struct lu_env *env, struct cl_io *io,
641 struct cl_page_list *queue,
642 struct ll_readahead_state *ras, bool hit,
645 struct vvp_io *vio = vvp_env_io(env);
646 struct ll_thread_info *lti = ll_env_info(env);
647 unsigned long pages, pages_min = 0;
648 pgoff_t ra_end_idx = 0, start_idx = 0, end_idx = 0;
650 struct ra_io_arg *ria = <i->lti_ria;
651 struct cl_object *clob;
657 inode = vvp_object_inode(clob);
659 memset(ria, 0, sizeof(*ria));
660 ret = ll_readahead_file_kms(env, io, &kms);
665 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
669 spin_lock(&ras->ras_lock);
672 * Note: other thread might rollback the ras_next_readahead_idx,
673 * if it can not get the full size of prepared pages, see the
674 * end of this function. For stride read ahead, it needs to
675 * make sure the offset is no less than ras_stride_offset,
676 * so that stride read ahead can work correctly.
678 if (stride_io_mode(ras))
679 start_idx = max_t(pgoff_t, ras->ras_next_readahead_idx,
680 ras->ras_stride_offset >> PAGE_SHIFT);
682 start_idx = ras->ras_next_readahead_idx;
684 if (ras->ras_window_pages > 0)
685 end_idx = ras->ras_window_start_idx + ras->ras_window_pages - 1;
687 /* Enlarge the RA window to encompass the full read */
688 if (vio->vui_ra_valid &&
689 end_idx < vio->vui_ra_start_idx + vio->vui_ra_pages - 1)
690 end_idx = vio->vui_ra_start_idx + vio->vui_ra_pages - 1;
695 /* Truncate RA window to end of file */
696 eof_index = (pgoff_t)((kms - 1) >> PAGE_SHIFT);
697 if (eof_index <= end_idx) {
702 ria->ria_start_idx = start_idx;
703 ria->ria_end_idx = end_idx;
704 /* If stride I/O mode is detected, get stride window*/
705 if (stride_io_mode(ras)) {
706 ria->ria_stoff = ras->ras_stride_offset;
707 ria->ria_length = ras->ras_stride_length;
708 ria->ria_bytes = ras->ras_stride_bytes;
710 spin_unlock(&ras->ras_lock);
713 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
716 pages = ria_page_count(ria);
718 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
723 CDEBUG(D_READA, DFID": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
724 PFID(lu_object_fid(&clob->co_lu)),
725 ria->ria_start_idx, ria->ria_end_idx,
726 vio->vui_ra_valid ? vio->vui_ra_start_idx : 0,
727 vio->vui_ra_valid ? vio->vui_ra_pages : 0,
730 /* at least to extend the readahead window to cover current read */
731 if (!hit && vio->vui_ra_valid &&
732 vio->vui_ra_start_idx + vio->vui_ra_pages > ria->ria_start_idx)
733 ria->ria_end_idx_min =
734 vio->vui_ra_start_idx + vio->vui_ra_pages - 1;
736 ria->ria_reserved = ll_ra_count_get(ll_i2sbi(inode), ria, pages,
738 if (ria->ria_reserved < pages)
739 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
741 CDEBUG(D_READA, "reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
742 ria->ria_reserved, pages, pages_min,
743 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
744 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
746 ret = ll_read_ahead_pages(env, io, queue, ras, ria, &ra_end_idx);
748 if (ria->ria_reserved != 0)
749 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
751 if (ra_end_idx == end_idx && ra_end_idx == (kms >> PAGE_SHIFT))
752 ll_ra_stats_inc(inode, RA_STAT_EOF);
755 "ra_end_idx = %lu end_idx = %lu stride end = %lu pages = %d\n",
756 ra_end_idx, end_idx, ria->ria_end_idx, ret);
758 if (ra_end_idx != end_idx)
759 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
760 if (ra_end_idx > 0) {
761 /* update the ras so that the next read-ahead tries from
762 * where we left off. */
763 spin_lock(&ras->ras_lock);
764 ras->ras_next_readahead_idx = ra_end_idx + 1;
765 spin_unlock(&ras->ras_lock);
772 static void ras_set_start(struct ll_readahead_state *ras, pgoff_t index)
774 ras->ras_window_start_idx = ras_align(ras, index);
777 /* called with the ras_lock held or from places where it doesn't matter */
778 static void ras_reset(struct ll_readahead_state *ras, pgoff_t index)
780 ras->ras_consecutive_requests = 0;
781 ras->ras_consecutive_bytes = 0;
782 ras->ras_window_pages = 0;
783 ras_set_start(ras, index);
784 ras->ras_next_readahead_idx = max(ras->ras_window_start_idx, index + 1);
789 /* called with the ras_lock held or from places where it doesn't matter */
790 static void ras_stride_reset(struct ll_readahead_state *ras)
792 ras->ras_consecutive_stride_requests = 0;
793 ras->ras_stride_length = 0;
794 ras->ras_stride_bytes = 0;
798 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
800 spin_lock_init(&ras->ras_lock);
801 ras->ras_rpc_pages = PTLRPC_MAX_BRW_PAGES;
803 ras->ras_last_read_end_bytes = 0;
804 ras->ras_requests = 0;
808 * Check whether the read request is in the stride window.
809 * If it is in the stride window, return true, otherwise return false.
811 static bool read_in_stride_window(struct ll_readahead_state *ras,
812 loff_t pos, loff_t count)
816 if (ras->ras_stride_length == 0 || ras->ras_stride_bytes == 0 ||
817 ras->ras_stride_bytes == ras->ras_stride_length)
820 stride_gap = pos - ras->ras_last_read_end_bytes - 1;
822 /* If it is contiguous read */
824 return ras->ras_consecutive_bytes + count <=
825 ras->ras_stride_bytes;
827 /* Otherwise check the stride by itself */
828 return (ras->ras_stride_length - ras->ras_stride_bytes) == stride_gap &&
829 ras->ras_consecutive_bytes == ras->ras_stride_bytes &&
830 count <= ras->ras_stride_bytes;
833 static void ras_init_stride_detector(struct ll_readahead_state *ras,
834 loff_t pos, loff_t count)
836 loff_t stride_gap = pos - ras->ras_last_read_end_bytes - 1;
838 LASSERT(ras->ras_consecutive_stride_requests == 0);
840 if (pos <= ras->ras_last_read_end_bytes) {
841 /*Reset stride window for forward read*/
842 ras_stride_reset(ras);
846 ras->ras_stride_bytes = ras->ras_consecutive_bytes;
847 ras->ras_stride_length = stride_gap + ras->ras_consecutive_bytes;
848 ras->ras_consecutive_stride_requests++;
849 ras->ras_stride_offset = pos;
855 stride_page_count(struct ll_readahead_state *ras, loff_t len)
858 stride_byte_count(ras->ras_stride_offset,
859 ras->ras_stride_length, ras->ras_stride_bytes,
860 ras->ras_window_start_idx << PAGE_SHIFT, len);
862 return (bytes_count + PAGE_SIZE - 1) >> PAGE_SHIFT;
865 /* Stride Read-ahead window will be increased inc_len according to
866 * stride I/O pattern */
867 static void ras_stride_increase_window(struct ll_readahead_state *ras,
868 struct ll_ra_info *ra, loff_t inc_bytes)
870 loff_t window_bytes, stride_bytes;
875 /* temporarily store in page units to reduce LASSERT() cost below */
876 end = ras->ras_window_start_idx + ras->ras_window_pages;
878 LASSERT(ras->ras_stride_length > 0);
879 LASSERTF(end >= (ras->ras_stride_offset >> PAGE_SHIFT),
880 "window_start_idx %lu, window_pages %lu stride_offset %llu\n",
881 ras->ras_window_start_idx, ras->ras_window_pages,
882 ras->ras_stride_offset);
885 if (end <= ras->ras_stride_offset)
888 stride_bytes = end - ras->ras_stride_offset;
890 div64_u64_rem(stride_bytes, ras->ras_stride_length, &left_bytes);
891 window_bytes = (ras->ras_window_pages << PAGE_SHIFT);
892 if (left_bytes < ras->ras_stride_bytes) {
893 if (ras->ras_stride_bytes - left_bytes >= inc_bytes) {
894 window_bytes += inc_bytes;
897 window_bytes += (ras->ras_stride_bytes - left_bytes);
898 inc_bytes -= (ras->ras_stride_bytes - left_bytes);
901 window_bytes += (ras->ras_stride_length - left_bytes);
904 LASSERT(ras->ras_stride_bytes != 0);
906 step = div64_u64_rem(inc_bytes, ras->ras_stride_bytes, &left_bytes);
908 window_bytes += step * ras->ras_stride_length + left_bytes;
909 LASSERT(window_bytes > 0);
912 if (stride_page_count(ras, window_bytes) <=
913 ra->ra_max_pages_per_file || ras->ras_window_pages == 0)
914 ras->ras_window_pages = (window_bytes >> PAGE_SHIFT);
916 LASSERT(ras->ras_window_pages > 0);
921 static void ras_increase_window(struct inode *inode,
922 struct ll_readahead_state *ras,
923 struct ll_ra_info *ra)
925 /* The stretch of ra-window should be aligned with max rpc_size
926 * but current clio architecture does not support retrieve such
927 * information from lower layer. FIXME later
929 if (stride_io_mode(ras)) {
930 ras_stride_increase_window(ras, ra,
931 (loff_t)ras->ras_rpc_pages << PAGE_SHIFT);
933 pgoff_t window_pages;
935 window_pages = min(ras->ras_window_pages + ras->ras_rpc_pages,
936 ra->ra_max_pages_per_file);
937 if (window_pages < ras->ras_rpc_pages)
938 ras->ras_window_pages = window_pages;
940 ras->ras_window_pages = ras_align(ras, window_pages);
945 * Seek within 8 pages are considered as sequential read for now.
947 static inline bool is_loose_seq_read(struct ll_readahead_state *ras, loff_t pos)
949 return pos_in_window(pos, ras->ras_last_read_end_bytes,
950 8UL << PAGE_SHIFT, 8UL << PAGE_SHIFT);
953 static void ras_detect_read_pattern(struct ll_readahead_state *ras,
954 struct ll_sb_info *sbi,
955 loff_t pos, size_t count, bool mmap)
957 bool stride_detect = false;
958 pgoff_t index = pos >> PAGE_SHIFT;
961 * Reset the read-ahead window in two cases. First when the app seeks
962 * or reads to some other part of the file. Secondly if we get a
963 * read-ahead miss that we think we've previously issued. This can
964 * be a symptom of there being so many read-ahead pages that the VM
965 * is reclaiming it before we get to it.
967 if (!is_loose_seq_read(ras, pos)) {
968 /* Check whether it is in stride I/O mode */
969 if (!read_in_stride_window(ras, pos, count)) {
970 if (ras->ras_consecutive_stride_requests == 0)
971 ras_init_stride_detector(ras, pos, count);
973 ras_stride_reset(ras);
974 ras->ras_consecutive_bytes = 0;
975 ras_reset(ras, index);
977 ras->ras_consecutive_bytes = 0;
978 ras->ras_consecutive_requests = 0;
979 if (++ras->ras_consecutive_stride_requests > 1)
980 stride_detect = true;
983 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
984 } else if (stride_io_mode(ras)) {
986 * If this is contiguous read but in stride I/O mode
987 * currently, check whether stride step still is valid,
988 * if invalid, it will reset the stride ra window to
991 if (!read_in_stride_window(ras, pos, count)) {
992 ras_stride_reset(ras);
993 ras->ras_window_pages = 0;
994 ras->ras_next_readahead_idx = index;
998 ras->ras_consecutive_bytes += count;
1000 pgoff_t idx = ras->ras_consecutive_bytes >> PAGE_SHIFT;
1002 if ((idx >= 4 && (idx & 3UL) == 0) || stride_detect)
1003 ras->ras_need_increase_window = true;
1004 } else if ((ras->ras_consecutive_requests > 1 || stride_detect)) {
1005 ras->ras_need_increase_window = true;
1008 ras->ras_last_read_end_bytes = pos + count - 1;
1011 void ll_ras_enter(struct file *f, loff_t pos, size_t count)
1013 struct ll_file_data *fd = f->private_data;
1014 struct ll_readahead_state *ras = &fd->fd_ras;
1015 struct inode *inode = file_inode(f);
1016 unsigned long index = pos >> PAGE_SHIFT;
1017 struct ll_sb_info *sbi = ll_i2sbi(inode);
1019 spin_lock(&ras->ras_lock);
1020 ras->ras_requests++;
1021 ras->ras_consecutive_requests++;
1022 ras->ras_need_increase_window = false;
1023 ras->ras_no_miss_check = false;
1025 * On the second access to a file smaller than the tunable
1026 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
1027 * file up to ra_max_pages_per_file. This is simply a best effort
1028 * and only occurs once per open file. Normal RA behavior is reverted
1029 * to for subsequent IO.
1031 if (ras->ras_requests >= 2) {
1033 struct ll_ra_info *ra = &sbi->ll_ra_info;
1035 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
1038 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
1039 ra->ra_max_read_ahead_whole_pages,
1040 ra->ra_max_pages_per_file);
1043 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
1044 ras->ras_window_start_idx = 0;
1045 ras->ras_next_readahead_idx = index + 1;
1046 ras->ras_window_pages = min(ra->ra_max_pages_per_file,
1047 ra->ra_max_read_ahead_whole_pages);
1048 ras->ras_no_miss_check = true;
1049 GOTO(out_unlock, 0);
1052 ras_detect_read_pattern(ras, sbi, pos, count, false);
1054 spin_unlock(&ras->ras_lock);
1057 static bool index_in_stride_window(struct ll_readahead_state *ras,
1060 loff_t pos = (loff_t)index << PAGE_SHIFT;
1062 if (ras->ras_stride_length == 0 || ras->ras_stride_bytes == 0 ||
1063 ras->ras_stride_bytes == ras->ras_stride_length)
1066 if (pos >= ras->ras_stride_offset) {
1069 div64_u64_rem(pos - ras->ras_stride_offset,
1070 ras->ras_stride_length, &offset);
1071 if (offset < ras->ras_stride_bytes ||
1072 ras->ras_stride_length - offset < PAGE_SIZE)
1074 } else if (ras->ras_stride_offset - pos < PAGE_SIZE) {
1082 * ll_ras_enter() is used to detect read pattern according to pos and count.
1084 * ras_update() is used to detect cache miss and
1085 * reset window or increase window accordingly
1087 static void ras_update(struct ll_sb_info *sbi, struct inode *inode,
1088 struct ll_readahead_state *ras, pgoff_t index,
1089 enum ras_update_flags flags)
1091 struct ll_ra_info *ra = &sbi->ll_ra_info;
1092 bool hit = flags & LL_RAS_HIT;
1095 spin_lock(&ras->ras_lock);
1098 CDEBUG(D_READA, DFID " pages at %lu miss.\n",
1099 PFID(ll_inode2fid(inode)), index);
1100 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
1103 * The readahead window has been expanded to cover whole
1104 * file size, we don't care whether ra miss happen or not.
1105 * Because we will read whole file to page cache even if
1106 * some pages missed.
1108 if (ras->ras_no_miss_check)
1109 GOTO(out_unlock, 0);
1111 if (flags & LL_RAS_MMAP)
1112 ras_detect_read_pattern(ras, sbi, (loff_t)index << PAGE_SHIFT,
1115 if (!hit && ras->ras_window_pages &&
1116 index < ras->ras_next_readahead_idx &&
1117 pos_in_window(index, ras->ras_window_start_idx, 0,
1118 ras->ras_window_pages)) {
1119 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
1120 ras->ras_need_increase_window = false;
1122 if (index_in_stride_window(ras, index) &&
1123 stride_io_mode(ras)) {
1125 * if (index != ras->ras_last_readpage + 1)
1126 * ras->ras_consecutive_pages = 0;
1128 ras_reset(ras, index);
1131 * If stride-RA hit cache miss, the stride
1132 * detector will not be reset to avoid the
1133 * overhead of redetecting read-ahead mode,
1134 * but on the condition that the stride window
1135 * is still intersect with normal sequential
1136 * read-ahead window.
1138 if (ras->ras_window_start_idx < ras->ras_stride_offset)
1139 ras_stride_reset(ras);
1143 * Reset both stride window and normal RA
1146 ras_reset(ras, index);
1147 /* ras->ras_consecutive_pages++; */
1148 ras->ras_consecutive_bytes = 0;
1149 ras_stride_reset(ras);
1150 GOTO(out_unlock, 0);
1153 ras_set_start(ras, index);
1155 if (stride_io_mode(ras)) {
1156 /* Since stride readahead is sentivite to the offset
1157 * of read-ahead, so we use original offset here,
1158 * instead of ras_window_start_idx, which is RPC aligned.
1160 ras->ras_next_readahead_idx = max(index + 1,
1161 ras->ras_next_readahead_idx);
1162 ras->ras_window_start_idx =
1163 max_t(pgoff_t, ras->ras_window_start_idx,
1164 ras->ras_stride_offset >> PAGE_SHIFT);
1166 if (ras->ras_next_readahead_idx < ras->ras_window_start_idx)
1167 ras->ras_next_readahead_idx = ras->ras_window_start_idx;
1169 ras->ras_next_readahead_idx = index + 1;
1172 if (ras->ras_need_increase_window) {
1173 ras_increase_window(inode, ras, ra);
1174 ras->ras_need_increase_window = false;
1179 spin_unlock(&ras->ras_lock);
1182 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1184 struct inode *inode = vmpage->mapping->host;
1185 struct ll_inode_info *lli = ll_i2info(inode);
1188 struct cl_page *page;
1189 struct cl_object *clob;
1190 bool redirtied = false;
1191 bool unlocked = false;
1196 LASSERT(PageLocked(vmpage));
1197 LASSERT(!PageWriteback(vmpage));
1199 LASSERT(ll_i2dtexp(inode) != NULL);
1201 env = cl_env_get(&refcheck);
1203 GOTO(out, result = PTR_ERR(env));
1205 clob = ll_i2info(inode)->lli_clob;
1206 LASSERT(clob != NULL);
1208 io = vvp_env_thread_io(env);
1210 io->ci_ignore_layout = 1;
1211 result = cl_io_init(env, io, CIT_MISC, clob);
1213 page = cl_page_find(env, clob, vmpage->index,
1214 vmpage, CPT_CACHEABLE);
1215 if (!IS_ERR(page)) {
1216 lu_ref_add(&page->cp_reference, "writepage",
1218 cl_page_assume(env, io, page);
1219 result = cl_page_flush(env, io, page);
1222 * Re-dirty page on error so it retries write,
1223 * but not in case when IO has actually
1224 * occurred and completed with an error.
1226 if (!PageError(vmpage)) {
1227 redirty_page_for_writepage(wbc, vmpage);
1232 cl_page_disown(env, io, page);
1234 lu_ref_del(&page->cp_reference,
1235 "writepage", current);
1236 cl_page_put(env, page);
1238 result = PTR_ERR(page);
1241 cl_io_fini(env, io);
1243 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1244 loff_t offset = cl_offset(clob, vmpage->index);
1246 /* Flush page failed because the extent is being written out.
1247 * Wait for the write of extent to be finished to avoid
1248 * breaking kernel which assumes ->writepage should mark
1249 * PageWriteback or clean the page. */
1250 result = cl_sync_file_range(inode, offset,
1251 offset + PAGE_SIZE - 1,
1254 /* actually we may have written more than one page.
1255 * decreasing this page because the caller will count
1257 wbc->nr_to_write -= result - 1;
1262 cl_env_put(env, &refcheck);
1267 if (!lli->lli_async_rc)
1268 lli->lli_async_rc = result;
1269 SetPageError(vmpage);
1271 unlock_page(vmpage);
1276 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1278 struct inode *inode = mapping->host;
1281 enum cl_fsync_mode mode;
1282 int range_whole = 0;
1286 if (wbc->range_cyclic) {
1287 start = (loff_t)mapping->writeback_index << PAGE_SHIFT;
1288 end = OBD_OBJECT_EOF;
1290 start = wbc->range_start;
1291 end = wbc->range_end;
1292 if (end == LLONG_MAX) {
1293 end = OBD_OBJECT_EOF;
1294 range_whole = start == 0;
1298 mode = CL_FSYNC_NONE;
1299 if (wbc->sync_mode == WB_SYNC_ALL)
1300 mode = CL_FSYNC_LOCAL;
1302 if (ll_i2info(inode)->lli_clob == NULL)
1305 /* for directio, it would call writepages() to evict cached pages
1306 * inside the IO context of write, which will cause deadlock at
1307 * layout_conf since it waits for active IOs to complete. */
1308 result = cl_sync_file_range(inode, start, end, mode, 1);
1310 wbc->nr_to_write -= result;
1314 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1315 if (end == OBD_OBJECT_EOF)
1316 mapping->writeback_index = 0;
1318 mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
1323 struct ll_cl_context *ll_cl_find(struct file *file)
1325 struct ll_file_data *fd = file->private_data;
1326 struct ll_cl_context *lcc;
1327 struct ll_cl_context *found = NULL;
1329 read_lock(&fd->fd_lock);
1330 list_for_each_entry(lcc, &fd->fd_lccs, lcc_list) {
1331 if (lcc->lcc_cookie == current) {
1336 read_unlock(&fd->fd_lock);
1341 void ll_cl_add(struct file *file, const struct lu_env *env, struct cl_io *io,
1344 struct ll_file_data *fd = file->private_data;
1345 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1347 memset(lcc, 0, sizeof(*lcc));
1348 INIT_LIST_HEAD(&lcc->lcc_list);
1349 lcc->lcc_cookie = current;
1352 lcc->lcc_type = type;
1354 write_lock(&fd->fd_lock);
1355 list_add(&lcc->lcc_list, &fd->fd_lccs);
1356 write_unlock(&fd->fd_lock);
1359 void ll_cl_remove(struct file *file, const struct lu_env *env)
1361 struct ll_file_data *fd = file->private_data;
1362 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1364 write_lock(&fd->fd_lock);
1365 list_del_init(&lcc->lcc_list);
1366 write_unlock(&fd->fd_lock);
1369 int ll_io_read_page(const struct lu_env *env, struct cl_io *io,
1370 struct cl_page *page, struct file *file)
1372 struct inode *inode = vvp_object_inode(page->cp_obj);
1373 struct ll_sb_info *sbi = ll_i2sbi(inode);
1374 struct ll_file_data *fd = file->private_data;
1375 struct ll_readahead_state *ras = &fd->fd_ras;
1376 struct cl_2queue *queue = &io->ci_queue;
1377 struct cl_sync_io *anchor = NULL;
1378 struct vvp_page *vpg;
1383 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1384 uptodate = vpg->vpg_defer_uptodate;
1386 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1387 sbi->ll_ra_info.ra_max_pages > 0 &&
1388 !vpg->vpg_ra_updated) {
1389 struct vvp_io *vio = vvp_env_io(env);
1390 enum ras_update_flags flags = 0;
1393 flags |= LL_RAS_HIT;
1394 if (!vio->vui_ra_valid)
1395 flags |= LL_RAS_MMAP;
1396 ras_update(sbi, inode, ras, vvp_index(vpg), flags);
1399 cl_2queue_init(queue);
1401 vpg->vpg_ra_used = 1;
1402 cl_page_export(env, page, 1);
1403 cl_page_disown(env, io, page);
1405 anchor = &vvp_env_info(env)->vti_anchor;
1406 cl_sync_io_init(anchor, 1);
1407 page->cp_sync_io = anchor;
1409 cl_2queue_add(queue, page);
1412 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1413 sbi->ll_ra_info.ra_max_pages > 0) {
1416 rc2 = ll_readahead(env, io, &queue->c2_qin, ras,
1418 CDEBUG(D_READA, DFID "%d pages read ahead at %lu\n",
1419 PFID(ll_inode2fid(inode)), rc2, vvp_index(vpg));
1422 if (queue->c2_qin.pl_nr > 0) {
1423 int count = queue->c2_qin.pl_nr;
1424 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
1426 task_io_account_read(PAGE_SIZE * count);
1430 if (anchor != NULL && !cl_page_is_owned(page, io)) { /* have sent */
1431 rc = cl_sync_io_wait(env, anchor, 0);
1433 cl_page_assume(env, io, page);
1434 cl_page_list_del(env, &queue->c2_qout, page);
1436 if (!PageUptodate(cl_page_vmpage(page))) {
1437 /* Failed to read a mirror, discard this page so that
1438 * new page can be created with new mirror.
1440 * TODO: this is not needed after page reinit
1441 * route is implemented */
1442 cl_page_discard(env, io, page);
1444 cl_page_disown(env, io, page);
1447 /* TODO: discard all pages until page reinit route is implemented */
1448 cl_page_list_discard(env, io, &queue->c2_qin);
1450 /* Unlock unsent read pages in case of error. */
1451 cl_page_list_disown(env, io, &queue->c2_qin);
1453 cl_2queue_fini(env, queue);
1459 * Possible return value:
1460 * 0 no async readahead triggered and fast read could not be used.
1461 * 1 no async readahead, but fast read could be used.
1462 * 2 async readahead triggered and fast read could be used too.
1465 static int kickoff_async_readahead(struct file *file, unsigned long pages)
1467 struct ll_readahead_work *lrw;
1468 struct inode *inode = file_inode(file);
1469 struct ll_sb_info *sbi = ll_i2sbi(inode);
1470 struct ll_file_data *fd = file->private_data;
1471 struct ll_readahead_state *ras = &fd->fd_ras;
1472 struct ll_ra_info *ra = &sbi->ll_ra_info;
1473 unsigned long throttle;
1474 pgoff_t start_idx = ras_align(ras, ras->ras_next_readahead_idx);
1475 pgoff_t end_idx = start_idx + pages - 1;
1477 throttle = min(ra->ra_async_pages_per_file_threshold,
1478 ra->ra_max_pages_per_file);
1480 * If this is strided i/o or the window is smaller than the
1481 * throttle limit, we do not do async readahead. Otherwise,
1482 * we do async readahead, allowing the user thread to do fast i/o.
1484 if (stride_io_mode(ras) || !throttle ||
1485 ras->ras_window_pages < throttle ||
1486 atomic_read(&ra->ra_async_inflight) > ra->ra_async_max_active)
1489 if ((atomic_read(&ra->ra_cur_pages) + pages) > ra->ra_max_pages)
1492 if (ras->ras_async_last_readpage_idx == start_idx)
1495 /* ll_readahead_work_free() free it */
1498 atomic_inc(&sbi->ll_ra_info.ra_async_inflight);
1499 lrw->lrw_file = get_file(file);
1500 lrw->lrw_start_idx = start_idx;
1501 lrw->lrw_end_idx = end_idx;
1502 spin_lock(&ras->ras_lock);
1503 ras->ras_next_readahead_idx = end_idx + 1;
1504 ras->ras_async_last_readpage_idx = start_idx;
1505 spin_unlock(&ras->ras_lock);
1506 ll_readahead_work_add(inode, lrw);
1515 * Check if we can issue a readahead RPC, if that is
1516 * the case, we can't do fast IO because we will need
1517 * a cl_io to issue the RPC.
1519 static bool ll_use_fast_io(struct file *file,
1520 struct ll_readahead_state *ras, pgoff_t index)
1522 unsigned long fast_read_pages =
1523 max(RA_REMAIN_WINDOW_MIN, ras->ras_rpc_pages);
1526 if (stride_io_mode(ras)) {
1527 skip_pages = (ras->ras_stride_length +
1528 ras->ras_stride_bytes - 1) / ras->ras_stride_bytes;
1529 skip_pages *= fast_read_pages;
1531 skip_pages = fast_read_pages;
1534 if (ras->ras_window_start_idx + ras->ras_window_pages <
1535 ras->ras_next_readahead_idx + skip_pages ||
1536 kickoff_async_readahead(file, fast_read_pages) > 0)
1542 int ll_readpage(struct file *file, struct page *vmpage)
1544 struct inode *inode = file_inode(file);
1545 struct cl_object *clob = ll_i2info(inode)->lli_clob;
1546 struct ll_cl_context *lcc;
1547 const struct lu_env *env = NULL;
1548 struct cl_io *io = NULL;
1549 struct cl_page *page;
1550 struct ll_sb_info *sbi = ll_i2sbi(inode);
1554 lcc = ll_cl_find(file);
1560 if (io == NULL) { /* fast read */
1561 struct inode *inode = file_inode(file);
1562 struct ll_file_data *fd = file->private_data;
1563 struct ll_readahead_state *ras = &fd->fd_ras;
1564 struct lu_env *local_env = NULL;
1565 struct vvp_page *vpg;
1569 /* TODO: need to verify the layout version to make sure
1570 * the page is not invalid due to layout change. */
1571 page = cl_vmpage_page(vmpage, clob);
1573 unlock_page(vmpage);
1574 ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1578 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1579 if (vpg->vpg_defer_uptodate) {
1580 enum ras_update_flags flags = LL_RAS_HIT;
1582 if (lcc && lcc->lcc_type == LCC_MMAP)
1583 flags |= LL_RAS_MMAP;
1585 /* For fast read, it updates read ahead state only
1586 * if the page is hit in cache because non cache page
1587 * case will be handled by slow read later. */
1588 ras_update(sbi, inode, ras, vvp_index(vpg), flags);
1589 /* avoid duplicate ras_update() call */
1590 vpg->vpg_ra_updated = 1;
1592 if (ll_use_fast_io(file, ras, vvp_index(vpg)))
1597 local_env = cl_env_percpu_get();
1601 /* export the page and skip io stack */
1603 vpg->vpg_ra_used = 1;
1604 cl_page_export(env, page, 1);
1606 ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1608 /* release page refcount before unlocking the page to ensure
1609 * the object won't be destroyed in the calling path of
1610 * cl_page_put(). Please see comment in ll_releasepage(). */
1611 cl_page_put(env, page);
1612 unlock_page(vmpage);
1614 cl_env_percpu_put(local_env);
1620 * Direct read can fall back to buffered read, but DIO is done
1621 * with lockless i/o, and buffered requires LDLM locking, so in
1622 * this case we must restart without lockless.
1624 if (file->f_flags & O_DIRECT &&
1625 lcc && lcc->lcc_type == LCC_RW &&
1626 !io->ci_ignore_lockless) {
1627 unlock_page(vmpage);
1628 io->ci_ignore_lockless = 1;
1629 io->ci_need_restart = 1;
1633 LASSERT(io->ci_state == CIS_IO_GOING);
1634 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
1635 if (!IS_ERR(page)) {
1636 LASSERT(page->cp_type == CPT_CACHEABLE);
1637 if (likely(!PageUptodate(vmpage))) {
1638 cl_page_assume(env, io, page);
1640 result = ll_io_read_page(env, io, page, file);
1642 /* Page from a non-object file. */
1643 unlock_page(vmpage);
1646 cl_page_put(env, page);
1648 unlock_page(vmpage);
1649 result = PTR_ERR(page);