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 static inline bool ll_readahead_enabled(struct ll_sb_info *sbi)
128 return sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
129 sbi->ll_ra_info.ra_max_pages > 0;
132 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
134 struct ll_sb_info *sbi = ll_i2sbi(inode);
135 ll_ra_stats_inc_sbi(sbi, which);
138 #define RAS_CDEBUG(ras) \
140 "lre %llu cr %lu cb %llu wsi %lu wp %lu nra %lu rpc %lu " \
141 "r %lu csr %lu so %llu sb %llu sl %llu lr %lu\n", \
142 ras->ras_last_read_end_bytes, ras->ras_consecutive_requests, \
143 ras->ras_consecutive_bytes, ras->ras_window_start_idx, \
144 ras->ras_window_pages, ras->ras_next_readahead_idx, \
145 ras->ras_rpc_pages, ras->ras_requests, \
146 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
147 ras->ras_stride_bytes, ras->ras_stride_length, \
148 ras->ras_async_last_readpage_idx)
150 static bool pos_in_window(loff_t pos, loff_t point,
151 unsigned long before, unsigned long after)
153 loff_t start = point - before;
154 loff_t end = point + after;
161 return start <= pos && pos <= end;
164 enum ll_ra_page_hint {
165 MAYNEED = 0, /* this page possibly accessed soon */
166 WILLNEED /* this page is gurateed to be needed */
170 * Initiates read-ahead of a page with given index.
172 * \retval +ve: page was already uptodate so it will be skipped
174 * \retval -ve: page wasn't added to \a queue for error;
175 * \retval 0: page was added into \a queue for read ahead.
177 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
178 struct cl_page_list *queue, pgoff_t index,
179 enum ll_ra_page_hint hint)
181 struct cl_object *clob = io->ci_obj;
182 struct inode *inode = vvp_object_inode(clob);
183 struct page *vmpage = NULL;
184 struct cl_page *page;
185 struct vvp_page *vpg;
186 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
188 const char *msg = NULL;
194 vmpage = grab_cache_page_nowait(inode->i_mapping, index);
195 if (vmpage == NULL) {
196 which = RA_STAT_FAILED_GRAB_PAGE;
197 msg = "g_c_p_n failed";
198 GOTO(out, rc = -EBUSY);
202 vmpage = find_or_create_page(inode->i_mapping, index,
205 GOTO(out, rc = -ENOMEM);
208 /* should not come here */
209 GOTO(out, rc = -EINVAL);
212 /* Check if vmpage was truncated or reclaimed */
213 if (vmpage->mapping != inode->i_mapping) {
214 which = RA_STAT_WRONG_GRAB_PAGE;
215 msg = "g_c_p_n returned invalid page";
216 GOTO(out, rc = -EBUSY);
219 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
221 which = RA_STAT_FAILED_GRAB_PAGE;
222 msg = "cl_page_find failed";
223 GOTO(out, rc = PTR_ERR(page));
226 lu_ref_add(&page->cp_reference, "ra", current);
227 cl_page_assume(env, io, page);
228 vpg = cl2vvp_page(cl_object_page_slice(clob, page));
229 if (!vpg->vpg_defer_uptodate && !PageUptodate(vmpage)) {
230 vpg->vpg_defer_uptodate = 1;
231 vpg->vpg_ra_used = 0;
232 cl_page_list_add(queue, page);
234 /* skip completed pages */
235 cl_page_unassume(env, io, page);
236 /* This page is already uptodate, returning a positive number
237 * to tell the callers about this */
241 lu_ref_del(&page->cp_reference, "ra", current);
242 cl_page_put(env, page);
245 if (vmpage != NULL) {
250 if (msg != NULL && hint == MAYNEED) {
251 ll_ra_stats_inc(inode, which);
252 CDEBUG(D_READA, "%s\n", msg);
259 #define RIA_DEBUG(ria) \
260 CDEBUG(D_READA, "rs %lu re %lu ro %llu rl %llu rb %llu\n", \
261 ria->ria_start_idx, ria->ria_end_idx, ria->ria_stoff, \
262 ria->ria_length, ria->ria_bytes)
264 static inline int stride_io_mode(struct ll_readahead_state *ras)
266 return ras->ras_consecutive_stride_requests > 1;
269 /* The function calculates how many bytes will be read in
270 * [off, off + length], in such stride IO area,
271 * stride_offset = st_off, stride_lengh = st_len,
272 * stride_bytes = st_bytes
274 * |------------------|*****|------------------|*****|------------|*****|....
277 * |----- st_len -----|
279 * How many bytes it should read in such pattern
280 * |-------------------------------------------------------------|
282 * |<------ length ------->|
284 * = |<----->| + |-------------------------------------| + |---|
285 * start_left st_bytes * i end_left
287 static loff_t stride_byte_count(loff_t st_off, loff_t st_len, loff_t st_bytes,
288 loff_t off, loff_t length)
290 u64 start = off > st_off ? off - st_off : 0;
291 u64 end = off + length > st_off ? off + length - st_off : 0;
296 if (st_len == 0 || length == 0 || end == 0)
299 start = div64_u64_rem(start, st_len, &start_left);
300 if (start_left < st_bytes)
301 start_left = st_bytes - start_left;
305 end = div64_u64_rem(end, st_len, &end_left);
306 if (end_left > st_bytes)
309 CDEBUG(D_READA, "start %llu, end %llu start_left %llu end_left %llu\n",
310 start, end, start_left, end_left);
313 bytes_count = end_left - (st_bytes - start_left);
315 bytes_count = start_left +
316 st_bytes * (end - start - 1) + end_left;
319 "st_off %llu, st_len %llu st_bytes %llu off %llu length %llu bytescount %llu\n",
320 st_off, st_len, st_bytes, off, length, bytes_count);
325 static unsigned long ria_page_count(struct ra_io_arg *ria)
327 loff_t length_bytes = ria->ria_end_idx >= ria->ria_start_idx ?
328 (loff_t)(ria->ria_end_idx -
329 ria->ria_start_idx + 1) << PAGE_SHIFT : 0;
332 if (ria->ria_length > ria->ria_bytes && ria->ria_bytes &&
333 (ria->ria_length & ~PAGE_MASK || ria->ria_bytes & ~PAGE_MASK ||
334 ria->ria_stoff & ~PAGE_MASK)) {
335 /* Over-estimate un-aligned page stride read */
336 unsigned long pg_count = ((ria->ria_bytes +
337 PAGE_SIZE - 1) >> PAGE_SHIFT) + 1;
338 pg_count *= length_bytes / ria->ria_length + 1;
342 bytes_count = stride_byte_count(ria->ria_stoff, ria->ria_length,
344 (loff_t)ria->ria_start_idx<<PAGE_SHIFT,
346 return (bytes_count + PAGE_SIZE - 1) >> PAGE_SHIFT;
349 static pgoff_t ras_align(struct ll_readahead_state *ras, pgoff_t index)
351 unsigned opt_size = min(ras->ras_window_pages, ras->ras_rpc_pages);
355 return index - (index % opt_size);
358 /* Check whether the index is in the defined ra-window */
359 static bool ras_inside_ra_window(pgoff_t idx, struct ra_io_arg *ria)
361 loff_t pos = (loff_t)idx << PAGE_SHIFT;
363 /* If ria_length == ria_bytes, it means non-stride I/O mode,
364 * idx should always inside read-ahead window in this case
365 * For stride I/O mode, just check whether the idx is inside
368 if (ria->ria_length == 0 || ria->ria_length == ria->ria_bytes)
371 if (pos >= ria->ria_stoff) {
374 div64_u64_rem(pos - ria->ria_stoff, ria->ria_length, &offset);
376 if (offset < ria->ria_bytes ||
377 (ria->ria_length - offset) < PAGE_SIZE)
379 } else if (pos + PAGE_SIZE > ria->ria_stoff) {
387 ll_read_ahead_pages(const struct lu_env *env, struct cl_io *io,
388 struct cl_page_list *queue, struct ll_readahead_state *ras,
389 struct ra_io_arg *ria, pgoff_t *ra_end)
391 struct cl_read_ahead ra = { 0 };
392 /* busy page count is per stride */
393 int rc = 0, count = 0, busy_page_count = 0;
396 LASSERT(ria != NULL);
399 for (page_idx = ria->ria_start_idx;
400 page_idx <= ria->ria_end_idx && ria->ria_reserved > 0;
402 if (ras_inside_ra_window(page_idx, ria)) {
403 if (ra.cra_end_idx == 0 || ra.cra_end_idx < page_idx) {
407 * Do not shrink ria_end_idx at any case until
408 * the minimum end of current read is covered.
410 * Do not extend read lock accross stripe if
411 * lock contention detected.
413 if (ra.cra_contention &&
414 page_idx > ria->ria_end_idx_min) {
415 ria->ria_end_idx = *ra_end;
419 cl_read_ahead_release(env, &ra);
421 rc = cl_io_read_ahead(env, io, page_idx, &ra);
426 * Only shrink ria_end_idx if the matched
427 * LDLM lock doesn't cover more.
429 if (page_idx > ra.cra_end_idx) {
430 ria->ria_end_idx = ra.cra_end_idx;
434 CDEBUG(D_READA, "idx: %lu, ra: %lu, rpc: %lu\n",
435 page_idx, ra.cra_end_idx,
437 LASSERTF(ra.cra_end_idx >= page_idx,
438 "object: %p, indcies %lu / %lu\n",
439 io->ci_obj, ra.cra_end_idx, page_idx);
440 /* update read ahead RPC size.
441 * NB: it's racy but doesn't matter */
442 if (ras->ras_rpc_pages != ra.cra_rpc_pages &&
443 ra.cra_rpc_pages > 0)
444 ras->ras_rpc_pages = ra.cra_rpc_pages;
445 /* trim it to align with optimal RPC size */
446 end_idx = ras_align(ras, ria->ria_end_idx + 1);
447 if (end_idx > 0 && !ria->ria_eof)
448 ria->ria_end_idx = end_idx - 1;
449 if (ria->ria_end_idx < ria->ria_end_idx_min)
450 ria->ria_end_idx = ria->ria_end_idx_min;
452 if (page_idx > ria->ria_end_idx)
455 /* If the page is inside the read-ahead window */
456 rc = ll_read_ahead_page(env, io, queue, page_idx,
458 if (rc < 0 && rc != -EBUSY)
463 "skip busy page: %lu\n", page_idx);
464 /* For page unaligned readahead the first
465 * last pages of each region can be read by
466 * another reader on the same node, and so
467 * may be busy. So only stop for > 2 busy
469 if (busy_page_count > 2)
474 /* Only subtract from reserve & count the page if we
475 * really did readahead on that page. */
480 } else if (stride_io_mode(ras)) {
481 /* If it is not in the read-ahead window, and it is
482 * read-ahead mode, then check whether it should skip
485 loff_t pos = (loff_t)page_idx << PAGE_SHIFT;
488 div64_u64_rem(pos - ria->ria_stoff, ria->ria_length,
490 if (offset >= ria->ria_bytes) {
491 pos += (ria->ria_length - offset);
492 if ((pos >> PAGE_SHIFT) >= page_idx + 1)
493 page_idx = (pos >> PAGE_SHIFT) - 1;
496 "Stride: jump %llu pages to %lu\n",
497 ria->ria_length - offset, page_idx);
503 cl_read_ahead_release(env, &ra);
508 static void ll_readahead_work_free(struct ll_readahead_work *work)
510 fput(work->lrw_file);
514 static void ll_readahead_handle_work(struct work_struct *wq);
515 static void ll_readahead_work_add(struct inode *inode,
516 struct ll_readahead_work *work)
518 INIT_WORK(&work->lrw_readahead_work, ll_readahead_handle_work);
519 queue_work(ll_i2sbi(inode)->ll_ra_info.ll_readahead_wq,
520 &work->lrw_readahead_work);
523 static int ll_readahead_file_kms(const struct lu_env *env,
524 struct cl_io *io, __u64 *kms)
526 struct cl_object *clob;
528 struct cl_attr *attr = vvp_env_thread_attr(env);
532 inode = vvp_object_inode(clob);
534 cl_object_attr_lock(clob);
535 ret = cl_object_attr_get(env, clob, attr);
536 cl_object_attr_unlock(clob);
541 *kms = attr->cat_kms;
545 static void ll_readahead_handle_work(struct work_struct *wq)
547 struct ll_readahead_work *work;
550 struct ra_io_arg *ria;
552 struct ll_file_data *fd;
553 struct ll_readahead_state *ras;
555 struct cl_2queue *queue;
556 pgoff_t ra_end_idx = 0;
557 unsigned long pages, pages_min = 0;
562 struct ll_sb_info *sbi;
564 work = container_of(wq, struct ll_readahead_work,
566 fd = work->lrw_file->private_data;
568 file = work->lrw_file;
569 inode = file_inode(file);
570 sbi = ll_i2sbi(inode);
572 env = cl_env_alloc(&refcheck, LCT_NOREF);
574 GOTO(out_free_work, rc = PTR_ERR(env));
576 io = vvp_env_thread_io(env);
577 ll_io_init(io, file, CIT_READ, NULL);
579 rc = ll_readahead_file_kms(env, io, &kms);
581 GOTO(out_put_env, rc);
584 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
585 GOTO(out_put_env, rc = 0);
588 ria = &ll_env_info(env)->lti_ria;
589 memset(ria, 0, sizeof(*ria));
591 ria->ria_start_idx = work->lrw_start_idx;
592 /* Truncate RA window to end of file */
593 eof_index = (pgoff_t)(kms - 1) >> PAGE_SHIFT;
594 if (eof_index <= work->lrw_end_idx) {
595 work->lrw_end_idx = eof_index;
598 if (work->lrw_end_idx <= work->lrw_start_idx)
599 GOTO(out_put_env, rc = 0);
601 ria->ria_end_idx = work->lrw_end_idx;
602 pages = ria->ria_end_idx - ria->ria_start_idx + 1;
603 ria->ria_reserved = ll_ra_count_get(sbi, ria,
604 ria_page_count(ria), pages_min);
607 "async reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
608 ria->ria_reserved, pages, pages_min,
609 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
610 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
612 if (ria->ria_reserved < pages) {
613 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
614 if (PAGES_TO_MiB(ria->ria_reserved) < 1) {
615 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
616 GOTO(out_put_env, rc = 0);
620 rc = cl_io_rw_init(env, io, CIT_READ, ria->ria_start_idx, pages);
622 GOTO(out_put_env, rc);
624 /* overwrite jobid inited in vvp_io_init() */
625 if (strncmp(ll_i2info(inode)->lli_jobid, work->lrw_jobid,
626 sizeof(work->lrw_jobid)))
627 memcpy(ll_i2info(inode)->lli_jobid, work->lrw_jobid,
628 sizeof(work->lrw_jobid));
630 vvp_env_io(env)->vui_io_subtype = IO_NORMAL;
631 vvp_env_io(env)->vui_fd = fd;
632 io->ci_state = CIS_LOCKED;
633 io->ci_async_readahead = true;
634 rc = cl_io_start(env, io);
636 GOTO(out_io_fini, rc);
638 queue = &io->ci_queue;
639 cl_2queue_init(queue);
641 rc = ll_read_ahead_pages(env, io, &queue->c2_qin, ras, ria,
643 if (ria->ria_reserved != 0)
644 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
645 if (queue->c2_qin.pl_nr > 0) {
646 int count = queue->c2_qin.pl_nr;
648 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
650 task_io_account_read(PAGE_SIZE * count);
652 if (ria->ria_end_idx == ra_end_idx && ra_end_idx == (kms >> PAGE_SHIFT))
653 ll_ra_stats_inc(inode, RA_STAT_EOF);
655 if (ra_end_idx != ria->ria_end_idx)
656 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
658 /* TODO: discard all pages until page reinit route is implemented */
659 cl_page_list_discard(env, io, &queue->c2_qin);
661 /* Unlock unsent read pages in case of error. */
662 cl_page_list_disown(env, io, &queue->c2_qin);
664 cl_2queue_fini(env, queue);
669 cl_env_put(env, &refcheck);
672 ll_ra_stats_inc_sbi(ll_i2sbi(inode), RA_STAT_ASYNC);
673 atomic_dec(&sbi->ll_ra_info.ra_async_inflight);
674 ll_readahead_work_free(work);
677 static int ll_readahead(const struct lu_env *env, struct cl_io *io,
678 struct cl_page_list *queue,
679 struct ll_readahead_state *ras, bool hit,
682 struct vvp_io *vio = vvp_env_io(env);
683 struct ll_thread_info *lti = ll_env_info(env);
684 unsigned long pages, pages_min = 0;
685 pgoff_t ra_end_idx = 0, start_idx = 0, end_idx = 0;
687 struct ra_io_arg *ria = <i->lti_ria;
688 struct cl_object *clob;
694 inode = vvp_object_inode(clob);
696 memset(ria, 0, sizeof(*ria));
697 ret = ll_readahead_file_kms(env, io, &kms);
702 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
706 spin_lock(&ras->ras_lock);
709 * Note: other thread might rollback the ras_next_readahead_idx,
710 * if it can not get the full size of prepared pages, see the
711 * end of this function. For stride read ahead, it needs to
712 * make sure the offset is no less than ras_stride_offset,
713 * so that stride read ahead can work correctly.
715 if (stride_io_mode(ras))
716 start_idx = max_t(pgoff_t, ras->ras_next_readahead_idx,
717 ras->ras_stride_offset >> PAGE_SHIFT);
719 start_idx = ras->ras_next_readahead_idx;
721 if (ras->ras_window_pages > 0)
722 end_idx = ras->ras_window_start_idx + ras->ras_window_pages - 1;
724 /* Enlarge the RA window to encompass the full read */
725 if (vio->vui_ra_valid &&
726 end_idx < vio->vui_ra_start_idx + vio->vui_ra_pages - 1)
727 end_idx = vio->vui_ra_start_idx + vio->vui_ra_pages - 1;
732 /* Truncate RA window to end of file */
733 eof_index = (pgoff_t)((kms - 1) >> PAGE_SHIFT);
734 if (eof_index <= end_idx) {
739 ria->ria_start_idx = start_idx;
740 ria->ria_end_idx = end_idx;
741 /* If stride I/O mode is detected, get stride window*/
742 if (stride_io_mode(ras)) {
743 ria->ria_stoff = ras->ras_stride_offset;
744 ria->ria_length = ras->ras_stride_length;
745 ria->ria_bytes = ras->ras_stride_bytes;
747 spin_unlock(&ras->ras_lock);
750 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
753 pages = ria_page_count(ria);
755 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
760 CDEBUG(D_READA, DFID": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
761 PFID(lu_object_fid(&clob->co_lu)),
762 ria->ria_start_idx, ria->ria_end_idx,
763 vio->vui_ra_valid ? vio->vui_ra_start_idx : 0,
764 vio->vui_ra_valid ? vio->vui_ra_pages : 0,
767 /* at least to extend the readahead window to cover current read */
768 if (!hit && vio->vui_ra_valid &&
769 vio->vui_ra_start_idx + vio->vui_ra_pages > ria->ria_start_idx) {
770 ria->ria_end_idx_min =
771 vio->vui_ra_start_idx + vio->vui_ra_pages - 1;
772 pages_min = vio->vui_ra_start_idx + vio->vui_ra_pages -
776 ria->ria_reserved = ll_ra_count_get(ll_i2sbi(inode), ria, pages,
778 if (ria->ria_reserved < pages)
779 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
781 CDEBUG(D_READA, "reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
782 ria->ria_reserved, pages, pages_min,
783 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
784 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
786 ret = ll_read_ahead_pages(env, io, queue, ras, ria, &ra_end_idx);
788 if (ria->ria_reserved != 0)
789 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
791 if (ra_end_idx == end_idx && ra_end_idx == (kms >> PAGE_SHIFT))
792 ll_ra_stats_inc(inode, RA_STAT_EOF);
795 "ra_end_idx = %lu end_idx = %lu stride end = %lu pages = %d\n",
796 ra_end_idx, end_idx, ria->ria_end_idx, ret);
798 if (ra_end_idx != end_idx)
799 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
800 if (ra_end_idx > 0) {
801 /* update the ras so that the next read-ahead tries from
802 * where we left off. */
803 spin_lock(&ras->ras_lock);
804 ras->ras_next_readahead_idx = ra_end_idx + 1;
805 spin_unlock(&ras->ras_lock);
812 static int ll_readpages(const struct lu_env *env, struct cl_io *io,
813 struct cl_page_list *queue,
814 pgoff_t start, pgoff_t end)
823 ret = ll_readahead_file_kms(env, io, &kms);
831 unsigned long end_index;
833 end_index = (unsigned long)((kms - 1) >> PAGE_SHIFT);
834 if (end_index <= end)
838 for (page_idx = start; page_idx <= end; page_idx++) {
839 ret= ll_read_ahead_page(env, io, queue, page_idx,
843 else if (ret == 0) /* ret 1 is already uptodate */
847 RETURN(count > 0 ? count : ret);
850 static void ras_set_start(struct ll_readahead_state *ras, pgoff_t index)
852 ras->ras_window_start_idx = ras_align(ras, index);
855 /* called with the ras_lock held or from places where it doesn't matter */
856 static void ras_reset(struct ll_readahead_state *ras, pgoff_t index)
858 ras->ras_consecutive_requests = 0;
859 ras->ras_consecutive_bytes = 0;
860 ras->ras_window_pages = 0;
861 ras_set_start(ras, index);
862 ras->ras_next_readahead_idx = max(ras->ras_window_start_idx, index + 1);
867 /* called with the ras_lock held or from places where it doesn't matter */
868 static void ras_stride_reset(struct ll_readahead_state *ras)
870 ras->ras_consecutive_stride_requests = 0;
871 ras->ras_stride_length = 0;
872 ras->ras_stride_bytes = 0;
876 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
878 spin_lock_init(&ras->ras_lock);
879 ras->ras_rpc_pages = PTLRPC_MAX_BRW_PAGES;
881 ras->ras_last_read_end_bytes = 0;
882 ras->ras_requests = 0;
886 * Check whether the read request is in the stride window.
887 * If it is in the stride window, return true, otherwise return false.
889 static bool read_in_stride_window(struct ll_readahead_state *ras,
890 loff_t pos, loff_t count)
894 if (ras->ras_stride_length == 0 || ras->ras_stride_bytes == 0 ||
895 ras->ras_stride_bytes == ras->ras_stride_length)
898 stride_gap = pos - ras->ras_last_read_end_bytes - 1;
900 /* If it is contiguous read */
902 return ras->ras_consecutive_bytes + count <=
903 ras->ras_stride_bytes;
905 /* Otherwise check the stride by itself */
906 return (ras->ras_stride_length - ras->ras_stride_bytes) == stride_gap &&
907 ras->ras_consecutive_bytes == ras->ras_stride_bytes &&
908 count <= ras->ras_stride_bytes;
911 static void ras_init_stride_detector(struct ll_readahead_state *ras,
912 loff_t pos, loff_t count)
914 loff_t stride_gap = pos - ras->ras_last_read_end_bytes - 1;
916 LASSERT(ras->ras_consecutive_stride_requests == 0);
918 if (pos <= ras->ras_last_read_end_bytes) {
919 /*Reset stride window for forward read*/
920 ras_stride_reset(ras);
924 ras->ras_stride_bytes = ras->ras_consecutive_bytes;
925 ras->ras_stride_length = stride_gap + ras->ras_consecutive_bytes;
926 ras->ras_consecutive_stride_requests++;
927 ras->ras_stride_offset = pos;
933 stride_page_count(struct ll_readahead_state *ras, loff_t len)
936 stride_byte_count(ras->ras_stride_offset,
937 ras->ras_stride_length, ras->ras_stride_bytes,
938 ras->ras_window_start_idx << PAGE_SHIFT, len);
940 return (bytes_count + PAGE_SIZE - 1) >> PAGE_SHIFT;
943 /* Stride Read-ahead window will be increased inc_len according to
944 * stride I/O pattern */
945 static void ras_stride_increase_window(struct ll_readahead_state *ras,
946 struct ll_ra_info *ra, loff_t inc_bytes)
948 loff_t window_bytes, stride_bytes;
953 /* temporarily store in page units to reduce LASSERT() cost below */
954 end = ras->ras_window_start_idx + ras->ras_window_pages;
956 LASSERT(ras->ras_stride_length > 0);
957 LASSERTF(end >= (ras->ras_stride_offset >> PAGE_SHIFT),
958 "window_start_idx %lu, window_pages %lu stride_offset %llu\n",
959 ras->ras_window_start_idx, ras->ras_window_pages,
960 ras->ras_stride_offset);
963 if (end <= ras->ras_stride_offset)
966 stride_bytes = end - ras->ras_stride_offset;
968 div64_u64_rem(stride_bytes, ras->ras_stride_length, &left_bytes);
969 window_bytes = (ras->ras_window_pages << PAGE_SHIFT);
970 if (left_bytes < ras->ras_stride_bytes) {
971 if (ras->ras_stride_bytes - left_bytes >= inc_bytes) {
972 window_bytes += inc_bytes;
975 window_bytes += (ras->ras_stride_bytes - left_bytes);
976 inc_bytes -= (ras->ras_stride_bytes - left_bytes);
979 window_bytes += (ras->ras_stride_length - left_bytes);
982 LASSERT(ras->ras_stride_bytes != 0);
984 step = div64_u64_rem(inc_bytes, ras->ras_stride_bytes, &left_bytes);
986 window_bytes += step * ras->ras_stride_length + left_bytes;
987 LASSERT(window_bytes > 0);
990 if (stride_page_count(ras, window_bytes) <=
991 ra->ra_max_pages_per_file || ras->ras_window_pages == 0)
992 ras->ras_window_pages = (window_bytes >> PAGE_SHIFT);
994 LASSERT(ras->ras_window_pages > 0);
999 static void ras_increase_window(struct inode *inode,
1000 struct ll_readahead_state *ras,
1001 struct ll_ra_info *ra)
1003 /* The stretch of ra-window should be aligned with max rpc_size
1004 * but current clio architecture does not support retrieve such
1005 * information from lower layer. FIXME later
1007 if (stride_io_mode(ras)) {
1008 ras_stride_increase_window(ras, ra,
1009 (loff_t)ras->ras_rpc_pages << PAGE_SHIFT);
1011 pgoff_t window_pages;
1013 window_pages = min(ras->ras_window_pages + ras->ras_rpc_pages,
1014 ra->ra_max_pages_per_file);
1015 if (window_pages < ras->ras_rpc_pages)
1016 ras->ras_window_pages = window_pages;
1018 ras->ras_window_pages = ras_align(ras, window_pages);
1023 * Seek within 8 pages are considered as sequential read for now.
1025 static inline bool is_loose_seq_read(struct ll_readahead_state *ras, loff_t pos)
1027 return pos_in_window(pos, ras->ras_last_read_end_bytes,
1028 8UL << PAGE_SHIFT, 8UL << PAGE_SHIFT);
1031 static void ras_detect_read_pattern(struct ll_readahead_state *ras,
1032 struct ll_sb_info *sbi,
1033 loff_t pos, size_t count, bool mmap)
1035 bool stride_detect = false;
1036 pgoff_t index = pos >> PAGE_SHIFT;
1039 * Reset the read-ahead window in two cases. First when the app seeks
1040 * or reads to some other part of the file. Secondly if we get a
1041 * read-ahead miss that we think we've previously issued. This can
1042 * be a symptom of there being so many read-ahead pages that the VM
1043 * is reclaiming it before we get to it.
1045 if (!is_loose_seq_read(ras, pos)) {
1046 /* Check whether it is in stride I/O mode */
1047 if (!read_in_stride_window(ras, pos, count)) {
1048 if (ras->ras_consecutive_stride_requests == 0)
1049 ras_init_stride_detector(ras, pos, count);
1051 ras_stride_reset(ras);
1052 ras->ras_consecutive_bytes = 0;
1053 ras_reset(ras, index);
1055 ras->ras_consecutive_bytes = 0;
1056 ras->ras_consecutive_requests = 0;
1057 if (++ras->ras_consecutive_stride_requests > 1)
1058 stride_detect = true;
1061 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
1062 } else if (stride_io_mode(ras)) {
1064 * If this is contiguous read but in stride I/O mode
1065 * currently, check whether stride step still is valid,
1066 * if invalid, it will reset the stride ra window to
1069 if (!read_in_stride_window(ras, pos, count)) {
1070 ras_stride_reset(ras);
1071 ras->ras_window_pages = 0;
1072 ras->ras_next_readahead_idx = index;
1076 ras->ras_consecutive_bytes += count;
1078 pgoff_t idx = ras->ras_consecutive_bytes >> PAGE_SHIFT;
1080 if ((idx >= 4 && (idx & 3UL) == 0) || stride_detect)
1081 ras->ras_need_increase_window = true;
1082 } else if ((ras->ras_consecutive_requests > 1 || stride_detect)) {
1083 ras->ras_need_increase_window = true;
1086 ras->ras_last_read_end_bytes = pos + count - 1;
1089 void ll_ras_enter(struct file *f, loff_t pos, size_t count)
1091 struct ll_file_data *fd = f->private_data;
1092 struct ll_readahead_state *ras = &fd->fd_ras;
1093 struct inode *inode = file_inode(f);
1094 unsigned long index = pos >> PAGE_SHIFT;
1095 struct ll_sb_info *sbi = ll_i2sbi(inode);
1097 spin_lock(&ras->ras_lock);
1098 ras->ras_requests++;
1099 ras->ras_consecutive_requests++;
1100 ras->ras_need_increase_window = false;
1101 ras->ras_no_miss_check = false;
1103 * On the second access to a file smaller than the tunable
1104 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
1105 * file up to ra_max_pages_per_file. This is simply a best effort
1106 * and only occurs once per open file. Normal RA behavior is reverted
1107 * to for subsequent IO.
1109 if (ras->ras_requests >= 2) {
1111 struct ll_ra_info *ra = &sbi->ll_ra_info;
1113 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
1116 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
1117 ra->ra_max_read_ahead_whole_pages,
1118 ra->ra_max_pages_per_file);
1121 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
1122 ras->ras_window_start_idx = 0;
1123 ras->ras_next_readahead_idx = index + 1;
1124 ras->ras_window_pages = min(ra->ra_max_pages_per_file,
1125 ra->ra_max_read_ahead_whole_pages);
1126 ras->ras_no_miss_check = true;
1127 GOTO(out_unlock, 0);
1130 ras_detect_read_pattern(ras, sbi, pos, count, false);
1132 spin_unlock(&ras->ras_lock);
1135 static bool index_in_stride_window(struct ll_readahead_state *ras,
1138 loff_t pos = (loff_t)index << PAGE_SHIFT;
1140 if (ras->ras_stride_length == 0 || ras->ras_stride_bytes == 0 ||
1141 ras->ras_stride_bytes == ras->ras_stride_length)
1144 if (pos >= ras->ras_stride_offset) {
1147 div64_u64_rem(pos - ras->ras_stride_offset,
1148 ras->ras_stride_length, &offset);
1149 if (offset < ras->ras_stride_bytes ||
1150 ras->ras_stride_length - offset < PAGE_SIZE)
1152 } else if (ras->ras_stride_offset - pos < PAGE_SIZE) {
1160 * ll_ras_enter() is used to detect read pattern according to pos and count.
1162 * ras_update() is used to detect cache miss and
1163 * reset window or increase window accordingly
1165 static void ras_update(struct ll_sb_info *sbi, struct inode *inode,
1166 struct ll_readahead_state *ras, pgoff_t index,
1167 enum ras_update_flags flags)
1169 struct ll_ra_info *ra = &sbi->ll_ra_info;
1170 bool hit = flags & LL_RAS_HIT;
1173 spin_lock(&ras->ras_lock);
1176 CDEBUG(D_READA, DFID " pages at %lu miss.\n",
1177 PFID(ll_inode2fid(inode)), index);
1178 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
1181 * The readahead window has been expanded to cover whole
1182 * file size, we don't care whether ra miss happen or not.
1183 * Because we will read whole file to page cache even if
1184 * some pages missed.
1186 if (ras->ras_no_miss_check)
1187 GOTO(out_unlock, 0);
1189 if (flags & LL_RAS_MMAP)
1190 ras_detect_read_pattern(ras, sbi, (loff_t)index << PAGE_SHIFT,
1193 if (!hit && ras->ras_window_pages &&
1194 index < ras->ras_next_readahead_idx &&
1195 pos_in_window(index, ras->ras_window_start_idx, 0,
1196 ras->ras_window_pages)) {
1197 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
1198 ras->ras_need_increase_window = false;
1200 if (index_in_stride_window(ras, index) &&
1201 stride_io_mode(ras)) {
1203 * if (index != ras->ras_last_readpage + 1)
1204 * ras->ras_consecutive_pages = 0;
1206 ras_reset(ras, index);
1209 * If stride-RA hit cache miss, the stride
1210 * detector will not be reset to avoid the
1211 * overhead of redetecting read-ahead mode,
1212 * but on the condition that the stride window
1213 * is still intersect with normal sequential
1214 * read-ahead window.
1216 if (ras->ras_window_start_idx < ras->ras_stride_offset)
1217 ras_stride_reset(ras);
1221 * Reset both stride window and normal RA
1224 ras_reset(ras, index);
1225 /* ras->ras_consecutive_pages++; */
1226 ras->ras_consecutive_bytes = 0;
1227 ras_stride_reset(ras);
1228 GOTO(out_unlock, 0);
1231 ras_set_start(ras, index);
1233 if (stride_io_mode(ras)) {
1234 /* Since stride readahead is sentivite to the offset
1235 * of read-ahead, so we use original offset here,
1236 * instead of ras_window_start_idx, which is RPC aligned.
1238 ras->ras_next_readahead_idx = max(index + 1,
1239 ras->ras_next_readahead_idx);
1240 ras->ras_window_start_idx =
1241 max_t(pgoff_t, ras->ras_window_start_idx,
1242 ras->ras_stride_offset >> PAGE_SHIFT);
1244 if (ras->ras_next_readahead_idx < ras->ras_window_start_idx)
1245 ras->ras_next_readahead_idx = ras->ras_window_start_idx;
1247 ras->ras_next_readahead_idx = index + 1;
1250 if (ras->ras_need_increase_window) {
1251 ras_increase_window(inode, ras, ra);
1252 ras->ras_need_increase_window = false;
1257 spin_unlock(&ras->ras_lock);
1260 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1262 struct inode *inode = vmpage->mapping->host;
1263 struct ll_inode_info *lli = ll_i2info(inode);
1266 struct cl_page *page;
1267 struct cl_object *clob;
1268 bool redirtied = false;
1269 bool unlocked = false;
1274 LASSERT(PageLocked(vmpage));
1275 LASSERT(!PageWriteback(vmpage));
1277 LASSERT(ll_i2dtexp(inode) != NULL);
1279 env = cl_env_get(&refcheck);
1281 GOTO(out, result = PTR_ERR(env));
1283 clob = ll_i2info(inode)->lli_clob;
1284 LASSERT(clob != NULL);
1286 io = vvp_env_thread_io(env);
1288 io->ci_ignore_layout = 1;
1289 result = cl_io_init(env, io, CIT_MISC, clob);
1291 page = cl_page_find(env, clob, vmpage->index,
1292 vmpage, CPT_CACHEABLE);
1293 if (!IS_ERR(page)) {
1294 lu_ref_add(&page->cp_reference, "writepage",
1296 cl_page_assume(env, io, page);
1297 result = cl_page_flush(env, io, page);
1300 * Re-dirty page on error so it retries write,
1301 * but not in case when IO has actually
1302 * occurred and completed with an error.
1304 if (!PageError(vmpage)) {
1305 redirty_page_for_writepage(wbc, vmpage);
1310 cl_page_disown(env, io, page);
1312 lu_ref_del(&page->cp_reference,
1313 "writepage", current);
1314 cl_page_put(env, page);
1316 result = PTR_ERR(page);
1319 cl_io_fini(env, io);
1321 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1322 loff_t offset = cl_offset(clob, vmpage->index);
1324 /* Flush page failed because the extent is being written out.
1325 * Wait for the write of extent to be finished to avoid
1326 * breaking kernel which assumes ->writepage should mark
1327 * PageWriteback or clean the page. */
1328 result = cl_sync_file_range(inode, offset,
1329 offset + PAGE_SIZE - 1,
1332 /* actually we may have written more than one page.
1333 * decreasing this page because the caller will count
1335 wbc->nr_to_write -= result - 1;
1340 cl_env_put(env, &refcheck);
1345 if (!lli->lli_async_rc)
1346 lli->lli_async_rc = result;
1347 SetPageError(vmpage);
1349 unlock_page(vmpage);
1354 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1356 struct inode *inode = mapping->host;
1359 enum cl_fsync_mode mode;
1360 int range_whole = 0;
1364 if (wbc->range_cyclic) {
1365 start = (loff_t)mapping->writeback_index << PAGE_SHIFT;
1366 end = OBD_OBJECT_EOF;
1368 start = wbc->range_start;
1369 end = wbc->range_end;
1370 if (end == LLONG_MAX) {
1371 end = OBD_OBJECT_EOF;
1372 range_whole = start == 0;
1376 mode = CL_FSYNC_NONE;
1377 if (wbc->sync_mode == WB_SYNC_ALL)
1378 mode = CL_FSYNC_LOCAL;
1380 if (ll_i2info(inode)->lli_clob == NULL)
1383 /* for directio, it would call writepages() to evict cached pages
1384 * inside the IO context of write, which will cause deadlock at
1385 * layout_conf since it waits for active IOs to complete. */
1386 result = cl_sync_file_range(inode, start, end, mode, 1);
1388 wbc->nr_to_write -= result;
1392 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1393 if (end == OBD_OBJECT_EOF)
1394 mapping->writeback_index = 0;
1396 mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
1401 struct ll_cl_context *ll_cl_find(struct file *file)
1403 struct ll_file_data *fd = file->private_data;
1404 struct ll_cl_context *lcc;
1405 struct ll_cl_context *found = NULL;
1407 read_lock(&fd->fd_lock);
1408 list_for_each_entry(lcc, &fd->fd_lccs, lcc_list) {
1409 if (lcc->lcc_cookie == current) {
1414 read_unlock(&fd->fd_lock);
1419 void ll_cl_add(struct file *file, const struct lu_env *env, struct cl_io *io,
1422 struct ll_file_data *fd = file->private_data;
1423 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1425 memset(lcc, 0, sizeof(*lcc));
1426 INIT_LIST_HEAD(&lcc->lcc_list);
1427 lcc->lcc_cookie = current;
1430 lcc->lcc_type = type;
1432 write_lock(&fd->fd_lock);
1433 list_add(&lcc->lcc_list, &fd->fd_lccs);
1434 write_unlock(&fd->fd_lock);
1437 void ll_cl_remove(struct file *file, const struct lu_env *env)
1439 struct ll_file_data *fd = file->private_data;
1440 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1442 write_lock(&fd->fd_lock);
1443 list_del_init(&lcc->lcc_list);
1444 write_unlock(&fd->fd_lock);
1447 int ll_io_read_page(const struct lu_env *env, struct cl_io *io,
1448 struct cl_page *page, struct file *file)
1450 struct inode *inode = vvp_object_inode(page->cp_obj);
1451 struct ll_sb_info *sbi = ll_i2sbi(inode);
1452 struct ll_file_data *fd = file->private_data;
1453 struct ll_readahead_state *ras = &fd->fd_ras;
1454 struct cl_2queue *queue = &io->ci_queue;
1455 struct cl_sync_io *anchor = NULL;
1456 struct vvp_page *vpg;
1457 int rc = 0, rc2 = 0;
1459 pgoff_t io_start_index;
1460 pgoff_t io_end_index;
1463 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1464 uptodate = vpg->vpg_defer_uptodate;
1466 if (ll_readahead_enabled(sbi) && !vpg->vpg_ra_updated) {
1467 struct vvp_io *vio = vvp_env_io(env);
1468 enum ras_update_flags flags = 0;
1471 flags |= LL_RAS_HIT;
1472 if (!vio->vui_ra_valid)
1473 flags |= LL_RAS_MMAP;
1474 ras_update(sbi, inode, ras, vvp_index(vpg), flags);
1477 cl_2queue_init(queue);
1479 vpg->vpg_ra_used = 1;
1480 cl_page_export(env, page, 1);
1481 cl_page_disown(env, io, page);
1483 anchor = &vvp_env_info(env)->vti_anchor;
1484 cl_sync_io_init(anchor, 1);
1485 page->cp_sync_io = anchor;
1487 cl_2queue_add(queue, page);
1490 io_start_index = cl_index(io->ci_obj, io->u.ci_rw.crw_pos);
1491 io_end_index = cl_index(io->ci_obj, io->u.ci_rw.crw_pos +
1492 io->u.ci_rw.crw_count - 1);
1493 if (ll_readahead_enabled(sbi)) {
1494 rc2 = ll_readahead(env, io, &queue->c2_qin, ras,
1496 CDEBUG(D_READA, DFID " %d pages read ahead at %lu\n",
1497 PFID(ll_inode2fid(inode)), rc2, vvp_index(vpg));
1498 } else if (vvp_index(vpg) == io_start_index &&
1499 io_end_index - io_start_index > 0) {
1500 rc2 = ll_readpages(env, io, &queue->c2_qin, io_start_index + 1,
1502 CDEBUG(D_READA, DFID " %d pages read at %lu\n",
1503 PFID(ll_inode2fid(inode)), rc2, vvp_index(vpg));
1506 if (queue->c2_qin.pl_nr > 0) {
1507 int count = queue->c2_qin.pl_nr;
1508 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
1510 task_io_account_read(PAGE_SIZE * count);
1514 if (anchor != NULL && !cl_page_is_owned(page, io)) { /* have sent */
1515 rc = cl_sync_io_wait(env, anchor, 0);
1517 cl_page_assume(env, io, page);
1518 cl_page_list_del(env, &queue->c2_qout, page);
1520 if (!PageUptodate(cl_page_vmpage(page))) {
1521 /* Failed to read a mirror, discard this page so that
1522 * new page can be created with new mirror.
1524 * TODO: this is not needed after page reinit
1525 * route is implemented */
1526 cl_page_discard(env, io, page);
1528 cl_page_disown(env, io, page);
1531 /* TODO: discard all pages until page reinit route is implemented */
1532 cl_page_list_discard(env, io, &queue->c2_qin);
1534 /* Unlock unsent read pages in case of error. */
1535 cl_page_list_disown(env, io, &queue->c2_qin);
1537 cl_2queue_fini(env, queue);
1543 * Possible return value:
1544 * 0 no async readahead triggered and fast read could not be used.
1545 * 1 no async readahead, but fast read could be used.
1546 * 2 async readahead triggered and fast read could be used too.
1549 static int kickoff_async_readahead(struct file *file, unsigned long pages)
1551 struct ll_readahead_work *lrw;
1552 struct inode *inode = file_inode(file);
1553 struct ll_sb_info *sbi = ll_i2sbi(inode);
1554 struct ll_file_data *fd = file->private_data;
1555 struct ll_readahead_state *ras = &fd->fd_ras;
1556 struct ll_ra_info *ra = &sbi->ll_ra_info;
1557 unsigned long throttle;
1558 pgoff_t start_idx = ras_align(ras, ras->ras_next_readahead_idx);
1559 pgoff_t end_idx = start_idx + pages - 1;
1561 throttle = min(ra->ra_async_pages_per_file_threshold,
1562 ra->ra_max_pages_per_file);
1564 * If this is strided i/o or the window is smaller than the
1565 * throttle limit, we do not do async readahead. Otherwise,
1566 * we do async readahead, allowing the user thread to do fast i/o.
1568 if (stride_io_mode(ras) || !throttle ||
1569 ras->ras_window_pages < throttle ||
1570 atomic_read(&ra->ra_async_inflight) > ra->ra_async_max_active)
1573 if ((atomic_read(&ra->ra_cur_pages) + pages) > ra->ra_max_pages)
1576 if (ras->ras_async_last_readpage_idx == start_idx)
1579 /* ll_readahead_work_free() free it */
1582 atomic_inc(&sbi->ll_ra_info.ra_async_inflight);
1583 lrw->lrw_file = get_file(file);
1584 lrw->lrw_start_idx = start_idx;
1585 lrw->lrw_end_idx = end_idx;
1586 spin_lock(&ras->ras_lock);
1587 ras->ras_next_readahead_idx = end_idx + 1;
1588 ras->ras_async_last_readpage_idx = start_idx;
1589 spin_unlock(&ras->ras_lock);
1590 memcpy(lrw->lrw_jobid, ll_i2info(inode)->lli_jobid,
1591 sizeof(lrw->lrw_jobid));
1592 ll_readahead_work_add(inode, lrw);
1601 * Check if we can issue a readahead RPC, if that is
1602 * the case, we can't do fast IO because we will need
1603 * a cl_io to issue the RPC.
1605 static bool ll_use_fast_io(struct file *file,
1606 struct ll_readahead_state *ras, pgoff_t index)
1608 unsigned long fast_read_pages =
1609 max(RA_REMAIN_WINDOW_MIN, ras->ras_rpc_pages);
1611 loff_t stride_bytes = ras->ras_stride_bytes;
1613 if (stride_io_mode(ras) && stride_bytes) {
1614 skip_pages = (ras->ras_stride_length +
1615 ras->ras_stride_bytes - 1) / stride_bytes;
1616 skip_pages *= fast_read_pages;
1618 skip_pages = fast_read_pages;
1621 if (ras->ras_window_start_idx + ras->ras_window_pages <
1622 ras->ras_next_readahead_idx + skip_pages ||
1623 kickoff_async_readahead(file, fast_read_pages) > 0)
1629 int ll_readpage(struct file *file, struct page *vmpage)
1631 struct inode *inode = file_inode(file);
1632 struct cl_object *clob = ll_i2info(inode)->lli_clob;
1633 struct ll_cl_context *lcc;
1634 const struct lu_env *env = NULL;
1635 struct cl_io *io = NULL;
1636 struct cl_page *page;
1637 struct ll_sb_info *sbi = ll_i2sbi(inode);
1641 lcc = ll_cl_find(file);
1647 if (io == NULL) { /* fast read */
1648 struct inode *inode = file_inode(file);
1649 struct ll_file_data *fd = file->private_data;
1650 struct ll_readahead_state *ras = &fd->fd_ras;
1651 struct lu_env *local_env = NULL;
1652 struct vvp_page *vpg;
1656 /* TODO: need to verify the layout version to make sure
1657 * the page is not invalid due to layout change. */
1658 page = cl_vmpage_page(vmpage, clob);
1660 unlock_page(vmpage);
1661 ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1665 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1666 if (vpg->vpg_defer_uptodate) {
1667 enum ras_update_flags flags = LL_RAS_HIT;
1669 if (lcc && lcc->lcc_type == LCC_MMAP)
1670 flags |= LL_RAS_MMAP;
1672 /* For fast read, it updates read ahead state only
1673 * if the page is hit in cache because non cache page
1674 * case will be handled by slow read later. */
1675 ras_update(sbi, inode, ras, vvp_index(vpg), flags);
1676 /* avoid duplicate ras_update() call */
1677 vpg->vpg_ra_updated = 1;
1679 if (ll_use_fast_io(file, ras, vvp_index(vpg)))
1684 local_env = cl_env_percpu_get();
1688 /* export the page and skip io stack */
1690 vpg->vpg_ra_used = 1;
1691 cl_page_export(env, page, 1);
1693 ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1695 /* release page refcount before unlocking the page to ensure
1696 * the object won't be destroyed in the calling path of
1697 * cl_page_put(). Please see comment in ll_releasepage(). */
1698 cl_page_put(env, page);
1699 unlock_page(vmpage);
1701 cl_env_percpu_put(local_env);
1707 * Direct read can fall back to buffered read, but DIO is done
1708 * with lockless i/o, and buffered requires LDLM locking, so in
1709 * this case we must restart without lockless.
1711 if (file->f_flags & O_DIRECT &&
1712 lcc && lcc->lcc_type == LCC_RW &&
1713 !io->ci_ignore_lockless) {
1714 unlock_page(vmpage);
1715 io->ci_ignore_lockless = 1;
1716 io->ci_need_restart = 1;
1720 LASSERT(io->ci_state == CIS_IO_GOING);
1721 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
1722 if (!IS_ERR(page)) {
1723 LASSERT(page->cp_type == CPT_CACHEABLE);
1724 if (likely(!PageUptodate(vmpage))) {
1725 cl_page_assume(env, io, page);
1727 result = ll_io_read_page(env, io, page, file);
1729 /* Page from a non-object file. */
1730 unlock_page(vmpage);
1733 cl_page_put(env, page);
1735 unlock_page(vmpage);
1736 result = PTR_ERR(page);