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) {
376 cl_read_ahead_release(env, &ra);
378 rc = cl_io_read_ahead(env, io, page_idx, &ra);
382 /* Do not shrink ria_end_idx at any case until
383 * the minimum end of current read is covered.
384 * And only shrink ria_end_idx if the matched
385 * LDLM lock doesn't cover more. */
386 if (page_idx > ra.cra_end_idx ||
387 (ra.cra_contention &&
388 page_idx > ria->ria_end_idx_min)) {
389 ria->ria_end_idx = ra.cra_end_idx;
393 CDEBUG(D_READA, "idx: %lu, ra: %lu, rpc: %lu\n",
394 page_idx, ra.cra_end_idx,
396 LASSERTF(ra.cra_end_idx >= page_idx,
397 "object: %p, indcies %lu / %lu\n",
398 io->ci_obj, ra.cra_end_idx, page_idx);
399 /* update read ahead RPC size.
400 * NB: it's racy but doesn't matter */
401 if (ras->ras_rpc_pages != ra.cra_rpc_pages &&
402 ra.cra_rpc_pages > 0)
403 ras->ras_rpc_pages = ra.cra_rpc_pages;
404 /* trim it to align with optimal RPC size */
405 end_idx = ras_align(ras, ria->ria_end_idx + 1);
406 if (end_idx > 0 && !ria->ria_eof)
407 ria->ria_end_idx = end_idx - 1;
408 if (ria->ria_end_idx < ria->ria_end_idx_min)
409 ria->ria_end_idx = ria->ria_end_idx_min;
411 if (page_idx > ria->ria_end_idx)
414 /* If the page is inside the read-ahead window */
415 rc = ll_read_ahead_page(env, io, queue, page_idx);
416 if (rc < 0 && rc != -EBUSY)
421 "skip busy page: %lu\n", page_idx);
422 /* For page unaligned readahead the first
423 * last pages of each region can be read by
424 * another reader on the same node, and so
425 * may be busy. So only stop for > 2 busy
427 if (busy_page_count > 2)
432 /* Only subtract from reserve & count the page if we
433 * really did readahead on that page. */
438 } else if (stride_io_mode(ras)) {
439 /* If it is not in the read-ahead window, and it is
440 * read-ahead mode, then check whether it should skip
443 loff_t pos = (loff_t)page_idx << PAGE_SHIFT;
446 div64_u64_rem(pos - ria->ria_stoff, ria->ria_length,
448 if (offset >= ria->ria_bytes) {
449 pos += (ria->ria_length - offset);
450 if ((pos >> PAGE_SHIFT) >= page_idx + 1)
451 page_idx = (pos >> PAGE_SHIFT) - 1;
454 "Stride: jump %llu pages to %lu\n",
455 ria->ria_length - offset, page_idx);
461 cl_read_ahead_release(env, &ra);
466 static void ll_readahead_work_free(struct ll_readahead_work *work)
468 fput(work->lrw_file);
472 static void ll_readahead_handle_work(struct work_struct *wq);
473 static void ll_readahead_work_add(struct inode *inode,
474 struct ll_readahead_work *work)
476 INIT_WORK(&work->lrw_readahead_work, ll_readahead_handle_work);
477 queue_work(ll_i2sbi(inode)->ll_ra_info.ll_readahead_wq,
478 &work->lrw_readahead_work);
481 static int ll_readahead_file_kms(const struct lu_env *env,
482 struct cl_io *io, __u64 *kms)
484 struct cl_object *clob;
486 struct cl_attr *attr = vvp_env_thread_attr(env);
490 inode = vvp_object_inode(clob);
492 cl_object_attr_lock(clob);
493 ret = cl_object_attr_get(env, clob, attr);
494 cl_object_attr_unlock(clob);
499 *kms = attr->cat_kms;
503 static void ll_readahead_handle_work(struct work_struct *wq)
505 struct ll_readahead_work *work;
508 struct ra_io_arg *ria;
510 struct ll_file_data *fd;
511 struct ll_readahead_state *ras;
513 struct cl_2queue *queue;
514 pgoff_t ra_end_idx = 0;
515 unsigned long pages, pages_min = 0;
521 work = container_of(wq, struct ll_readahead_work,
523 fd = LUSTRE_FPRIVATE(work->lrw_file);
525 file = work->lrw_file;
526 inode = file_inode(file);
528 env = cl_env_alloc(&refcheck, LCT_NOREF);
530 GOTO(out_free_work, rc = PTR_ERR(env));
532 io = vvp_env_thread_io(env);
533 ll_io_init(io, file, CIT_READ, NULL);
535 rc = ll_readahead_file_kms(env, io, &kms);
537 GOTO(out_put_env, rc);
540 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
541 GOTO(out_put_env, rc = 0);
544 ria = &ll_env_info(env)->lti_ria;
545 memset(ria, 0, sizeof(*ria));
547 ria->ria_start_idx = work->lrw_start_idx;
548 /* Truncate RA window to end of file */
549 eof_index = (pgoff_t)(kms - 1) >> PAGE_SHIFT;
550 if (eof_index <= work->lrw_end_idx) {
551 work->lrw_end_idx = eof_index;
554 if (work->lrw_end_idx <= work->lrw_start_idx)
555 GOTO(out_put_env, rc = 0);
557 ria->ria_end_idx = work->lrw_end_idx;
558 pages = ria->ria_end_idx - ria->ria_start_idx + 1;
559 ria->ria_reserved = ll_ra_count_get(ll_i2sbi(inode), ria,
560 ria_page_count(ria), pages_min);
563 "async reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
564 ria->ria_reserved, pages, pages_min,
565 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
566 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
568 if (ria->ria_reserved < pages) {
569 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
570 if (PAGES_TO_MiB(ria->ria_reserved) < 1) {
571 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
572 GOTO(out_put_env, rc = 0);
576 rc = cl_io_rw_init(env, io, CIT_READ, ria->ria_start_idx, pages);
578 GOTO(out_put_env, rc);
580 vvp_env_io(env)->vui_io_subtype = IO_NORMAL;
581 vvp_env_io(env)->vui_fd = fd;
582 io->ci_state = CIS_LOCKED;
583 io->ci_async_readahead = true;
584 rc = cl_io_start(env, io);
586 GOTO(out_io_fini, rc);
588 queue = &io->ci_queue;
589 cl_2queue_init(queue);
591 rc = ll_read_ahead_pages(env, io, &queue->c2_qin, ras, ria,
593 if (ria->ria_reserved != 0)
594 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
595 if (queue->c2_qin.pl_nr > 0) {
596 int count = queue->c2_qin.pl_nr;
598 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
600 task_io_account_read(PAGE_SIZE * count);
602 if (ria->ria_end_idx == ra_end_idx && ra_end_idx == (kms >> PAGE_SHIFT))
603 ll_ra_stats_inc(inode, RA_STAT_EOF);
605 if (ra_end_idx != ria->ria_end_idx)
606 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
608 /* TODO: discard all pages until page reinit route is implemented */
609 cl_page_list_discard(env, io, &queue->c2_qin);
611 /* Unlock unsent read pages in case of error. */
612 cl_page_list_disown(env, io, &queue->c2_qin);
614 cl_2queue_fini(env, queue);
619 cl_env_put(env, &refcheck);
622 ll_ra_stats_inc_sbi(ll_i2sbi(inode), RA_STAT_ASYNC);
623 ll_readahead_work_free(work);
626 static int ll_readahead(const struct lu_env *env, struct cl_io *io,
627 struct cl_page_list *queue,
628 struct ll_readahead_state *ras, bool hit,
631 struct vvp_io *vio = vvp_env_io(env);
632 struct ll_thread_info *lti = ll_env_info(env);
633 unsigned long pages, pages_min = 0;
634 pgoff_t ra_end_idx = 0, start_idx = 0, end_idx = 0;
636 struct ra_io_arg *ria = <i->lti_ria;
637 struct cl_object *clob;
643 inode = vvp_object_inode(clob);
645 memset(ria, 0, sizeof(*ria));
646 ret = ll_readahead_file_kms(env, io, &kms);
651 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
655 spin_lock(&ras->ras_lock);
658 * Note: other thread might rollback the ras_next_readahead_idx,
659 * if it can not get the full size of prepared pages, see the
660 * end of this function. For stride read ahead, it needs to
661 * make sure the offset is no less than ras_stride_offset,
662 * so that stride read ahead can work correctly.
664 if (stride_io_mode(ras))
665 start_idx = max_t(pgoff_t, ras->ras_next_readahead_idx,
666 ras->ras_stride_offset >> PAGE_SHIFT);
668 start_idx = ras->ras_next_readahead_idx;
670 if (ras->ras_window_pages > 0)
671 end_idx = ras->ras_window_start_idx + ras->ras_window_pages - 1;
673 /* Enlarge the RA window to encompass the full read */
674 if (vio->vui_ra_valid &&
675 end_idx < vio->vui_ra_start_idx + vio->vui_ra_pages - 1)
676 end_idx = vio->vui_ra_start_idx + vio->vui_ra_pages - 1;
681 /* Truncate RA window to end of file */
682 eof_index = (pgoff_t)((kms - 1) >> PAGE_SHIFT);
683 if (eof_index <= end_idx) {
688 ria->ria_start_idx = start_idx;
689 ria->ria_end_idx = end_idx;
690 /* If stride I/O mode is detected, get stride window*/
691 if (stride_io_mode(ras)) {
692 ria->ria_stoff = ras->ras_stride_offset;
693 ria->ria_length = ras->ras_stride_length;
694 ria->ria_bytes = ras->ras_stride_bytes;
696 spin_unlock(&ras->ras_lock);
699 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
702 pages = ria_page_count(ria);
704 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
709 CDEBUG(D_READA, DFID": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
710 PFID(lu_object_fid(&clob->co_lu)),
711 ria->ria_start_idx, ria->ria_end_idx,
712 vio->vui_ra_valid ? vio->vui_ra_start_idx : 0,
713 vio->vui_ra_valid ? vio->vui_ra_pages : 0,
716 /* at least to extend the readahead window to cover current read */
717 if (!hit && vio->vui_ra_valid &&
718 vio->vui_ra_start_idx + vio->vui_ra_pages > ria->ria_start_idx)
719 ria->ria_end_idx_min =
720 vio->vui_ra_start_idx + vio->vui_ra_pages - 1;
722 ria->ria_reserved = ll_ra_count_get(ll_i2sbi(inode), ria, pages,
724 if (ria->ria_reserved < pages)
725 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
727 CDEBUG(D_READA, "reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
728 ria->ria_reserved, pages, pages_min,
729 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
730 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
732 ret = ll_read_ahead_pages(env, io, queue, ras, ria, &ra_end_idx);
734 if (ria->ria_reserved != 0)
735 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
737 if (ra_end_idx == end_idx && ra_end_idx == (kms >> PAGE_SHIFT))
738 ll_ra_stats_inc(inode, RA_STAT_EOF);
741 "ra_end_idx = %lu end_idx = %lu stride end = %lu pages = %d\n",
742 ra_end_idx, end_idx, ria->ria_end_idx, ret);
744 if (ra_end_idx != end_idx)
745 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
746 if (ra_end_idx > 0) {
747 /* update the ras so that the next read-ahead tries from
748 * where we left off. */
749 spin_lock(&ras->ras_lock);
750 ras->ras_next_readahead_idx = ra_end_idx + 1;
751 spin_unlock(&ras->ras_lock);
758 static void ras_set_start(struct ll_readahead_state *ras, pgoff_t index)
760 ras->ras_window_start_idx = ras_align(ras, index);
763 /* called with the ras_lock held or from places where it doesn't matter */
764 static void ras_reset(struct ll_readahead_state *ras, pgoff_t index)
766 ras->ras_consecutive_requests = 0;
767 ras->ras_consecutive_bytes = 0;
768 ras->ras_window_pages = 0;
769 ras_set_start(ras, index);
770 ras->ras_next_readahead_idx = max(ras->ras_window_start_idx, index + 1);
775 /* called with the ras_lock held or from places where it doesn't matter */
776 static void ras_stride_reset(struct ll_readahead_state *ras)
778 ras->ras_consecutive_stride_requests = 0;
779 ras->ras_stride_length = 0;
780 ras->ras_stride_bytes = 0;
784 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
786 spin_lock_init(&ras->ras_lock);
787 ras->ras_rpc_pages = PTLRPC_MAX_BRW_PAGES;
789 ras->ras_last_read_end_bytes = 0;
790 ras->ras_requests = 0;
794 * Check whether the read request is in the stride window.
795 * If it is in the stride window, return true, otherwise return false.
797 static bool read_in_stride_window(struct ll_readahead_state *ras,
798 loff_t pos, loff_t count)
802 if (ras->ras_stride_length == 0 || ras->ras_stride_bytes == 0 ||
803 ras->ras_stride_bytes == ras->ras_stride_length)
806 stride_gap = pos - ras->ras_last_read_end_bytes - 1;
808 /* If it is contiguous read */
810 return ras->ras_consecutive_bytes + count <=
811 ras->ras_stride_bytes;
813 /* Otherwise check the stride by itself */
814 return (ras->ras_stride_length - ras->ras_stride_bytes) == stride_gap &&
815 ras->ras_consecutive_bytes == ras->ras_stride_bytes &&
816 count <= ras->ras_stride_bytes;
819 static void ras_init_stride_detector(struct ll_readahead_state *ras,
820 loff_t pos, loff_t count)
822 loff_t stride_gap = pos - ras->ras_last_read_end_bytes - 1;
824 LASSERT(ras->ras_consecutive_stride_requests == 0);
826 if (pos <= ras->ras_last_read_end_bytes) {
827 /*Reset stride window for forward read*/
828 ras_stride_reset(ras);
832 ras->ras_stride_bytes = ras->ras_consecutive_bytes;
833 ras->ras_stride_length = stride_gap + ras->ras_consecutive_bytes;
834 ras->ras_consecutive_stride_requests++;
835 ras->ras_stride_offset = pos;
841 stride_page_count(struct ll_readahead_state *ras, loff_t len)
844 stride_byte_count(ras->ras_stride_offset,
845 ras->ras_stride_length, ras->ras_stride_bytes,
846 ras->ras_stride_offset, len);
848 return (bytes_count + PAGE_SIZE - 1) >> PAGE_SHIFT;
851 /* Stride Read-ahead window will be increased inc_len according to
852 * stride I/O pattern */
853 static void ras_stride_increase_window(struct ll_readahead_state *ras,
854 struct ll_ra_info *ra, loff_t inc_bytes)
856 loff_t window_bytes, stride_bytes;
861 /* temporarily store in page units to reduce LASSERT() cost below */
862 end = ras->ras_window_start_idx + ras->ras_window_pages;
864 LASSERT(ras->ras_stride_length > 0);
865 LASSERTF(end >= (ras->ras_stride_offset >> PAGE_SHIFT),
866 "window_start_idx %lu, window_pages %lu stride_offset %llu\n",
867 ras->ras_window_start_idx, ras->ras_window_pages,
868 ras->ras_stride_offset);
871 if (end <= ras->ras_stride_offset)
874 stride_bytes = end - ras->ras_stride_offset;
876 div64_u64_rem(stride_bytes, ras->ras_stride_length, &left_bytes);
877 window_bytes = ((loff_t)ras->ras_window_pages << PAGE_SHIFT) -
880 if (left_bytes < ras->ras_stride_bytes)
881 left_bytes += inc_bytes;
883 left_bytes = ras->ras_stride_bytes + inc_bytes;
885 LASSERT(ras->ras_stride_bytes != 0);
887 step = div64_u64_rem(left_bytes, ras->ras_stride_bytes, &left_bytes);
889 window_bytes += step * ras->ras_stride_length + left_bytes;
891 if (stride_page_count(ras, window_bytes) <= ra->ra_max_pages_per_file)
892 ras->ras_window_pages = (window_bytes >> PAGE_SHIFT);
897 static void ras_increase_window(struct inode *inode,
898 struct ll_readahead_state *ras,
899 struct ll_ra_info *ra)
901 /* The stretch of ra-window should be aligned with max rpc_size
902 * but current clio architecture does not support retrieve such
903 * information from lower layer. FIXME later
905 if (stride_io_mode(ras)) {
906 ras_stride_increase_window(ras, ra,
907 (loff_t)ras->ras_rpc_pages << PAGE_SHIFT);
909 pgoff_t window_pages;
911 window_pages = min(ras->ras_window_pages + ras->ras_rpc_pages,
912 ra->ra_max_pages_per_file);
913 if (window_pages < ras->ras_rpc_pages)
914 ras->ras_window_pages = window_pages;
916 ras->ras_window_pages = ras_align(ras, window_pages);
921 * Seek within 8 pages are considered as sequential read for now.
923 static inline bool is_loose_seq_read(struct ll_readahead_state *ras, loff_t pos)
925 return pos_in_window(pos, ras->ras_last_read_end_bytes,
926 8UL << PAGE_SHIFT, 8UL << PAGE_SHIFT);
929 static void ras_detect_read_pattern(struct ll_readahead_state *ras,
930 struct ll_sb_info *sbi,
931 loff_t pos, size_t count, bool mmap)
933 bool stride_detect = false;
934 pgoff_t index = pos >> PAGE_SHIFT;
937 * Reset the read-ahead window in two cases. First when the app seeks
938 * or reads to some other part of the file. Secondly if we get a
939 * read-ahead miss that we think we've previously issued. This can
940 * be a symptom of there being so many read-ahead pages that the VM
941 * is reclaiming it before we get to it.
943 if (!is_loose_seq_read(ras, pos)) {
944 /* Check whether it is in stride I/O mode */
945 if (!read_in_stride_window(ras, pos, count)) {
946 if (ras->ras_consecutive_stride_requests == 0)
947 ras_init_stride_detector(ras, pos, count);
949 ras_stride_reset(ras);
950 ras->ras_consecutive_bytes = 0;
951 ras_reset(ras, index);
953 ras->ras_consecutive_bytes = 0;
954 ras->ras_consecutive_requests = 0;
955 if (++ras->ras_consecutive_stride_requests > 1)
956 stride_detect = true;
959 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
960 } else if (stride_io_mode(ras)) {
962 * If this is contiguous read but in stride I/O mode
963 * currently, check whether stride step still is valid,
964 * if invalid, it will reset the stride ra window to
967 if (!read_in_stride_window(ras, pos, count)) {
968 ras_stride_reset(ras);
969 ras->ras_window_pages = 0;
970 ras->ras_next_readahead_idx = index;
974 ras->ras_consecutive_bytes += count;
976 pgoff_t idx = ras->ras_consecutive_bytes >> PAGE_SHIFT;
978 if ((idx >= 4 && (idx & 3UL) == 0) || stride_detect)
979 ras->ras_need_increase_window = true;
980 } else if ((ras->ras_consecutive_requests > 1 || stride_detect)) {
981 ras->ras_need_increase_window = true;
984 ras->ras_last_read_end_bytes = pos + count - 1;
987 void ll_ras_enter(struct file *f, loff_t pos, size_t count)
989 struct ll_file_data *fd = LUSTRE_FPRIVATE(f);
990 struct ll_readahead_state *ras = &fd->fd_ras;
991 struct inode *inode = file_inode(f);
992 unsigned long index = pos >> PAGE_SHIFT;
993 struct ll_sb_info *sbi = ll_i2sbi(inode);
995 spin_lock(&ras->ras_lock);
997 ras->ras_consecutive_requests++;
998 ras->ras_need_increase_window = false;
999 ras->ras_no_miss_check = false;
1001 * On the second access to a file smaller than the tunable
1002 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
1003 * file up to ra_max_pages_per_file. This is simply a best effort
1004 * and only occurs once per open file. Normal RA behavior is reverted
1005 * to for subsequent IO.
1007 if (ras->ras_requests >= 2) {
1009 struct ll_ra_info *ra = &sbi->ll_ra_info;
1011 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
1014 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
1015 ra->ra_max_read_ahead_whole_pages,
1016 ra->ra_max_pages_per_file);
1019 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
1020 ras->ras_window_start_idx = 0;
1021 ras->ras_next_readahead_idx = index + 1;
1022 ras->ras_window_pages = min(ra->ra_max_pages_per_file,
1023 ra->ra_max_read_ahead_whole_pages);
1024 ras->ras_no_miss_check = true;
1025 GOTO(out_unlock, 0);
1028 ras_detect_read_pattern(ras, sbi, pos, count, false);
1030 spin_unlock(&ras->ras_lock);
1033 static bool index_in_stride_window(struct ll_readahead_state *ras,
1036 loff_t pos = (loff_t)index << PAGE_SHIFT;
1038 if (ras->ras_stride_length == 0 || ras->ras_stride_bytes == 0 ||
1039 ras->ras_stride_bytes == ras->ras_stride_length)
1042 if (pos >= ras->ras_stride_offset) {
1045 div64_u64_rem(pos - ras->ras_stride_offset,
1046 ras->ras_stride_length, &offset);
1047 if (offset < ras->ras_stride_bytes ||
1048 ras->ras_stride_length - offset < PAGE_SIZE)
1050 } else if (ras->ras_stride_offset - pos < PAGE_SIZE) {
1058 * ll_ras_enter() is used to detect read pattern according to pos and count.
1060 * ras_update() is used to detect cache miss and
1061 * reset window or increase window accordingly
1063 static void ras_update(struct ll_sb_info *sbi, struct inode *inode,
1064 struct ll_readahead_state *ras, pgoff_t index,
1065 enum ras_update_flags flags)
1067 struct ll_ra_info *ra = &sbi->ll_ra_info;
1068 bool hit = flags & LL_RAS_HIT;
1071 spin_lock(&ras->ras_lock);
1074 CDEBUG(D_READA, DFID " pages at %lu miss.\n",
1075 PFID(ll_inode2fid(inode)), index);
1076 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
1079 * The readahead window has been expanded to cover whole
1080 * file size, we don't care whether ra miss happen or not.
1081 * Because we will read whole file to page cache even if
1082 * some pages missed.
1084 if (ras->ras_no_miss_check)
1085 GOTO(out_unlock, 0);
1087 if (flags & LL_RAS_MMAP)
1088 ras_detect_read_pattern(ras, sbi, (loff_t)index << PAGE_SHIFT,
1091 if (!hit && ras->ras_window_pages &&
1092 index < ras->ras_next_readahead_idx &&
1093 pos_in_window(index, ras->ras_window_start_idx, 0,
1094 ras->ras_window_pages)) {
1095 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
1096 ras->ras_need_increase_window = false;
1098 if (index_in_stride_window(ras, index) &&
1099 stride_io_mode(ras)) {
1101 * if (index != ras->ras_last_readpage + 1)
1102 * ras->ras_consecutive_pages = 0;
1104 ras_reset(ras, index);
1107 * If stride-RA hit cache miss, the stride
1108 * detector will not be reset to avoid the
1109 * overhead of redetecting read-ahead mode,
1110 * but on the condition that the stride window
1111 * is still intersect with normal sequential
1112 * read-ahead window.
1114 if (ras->ras_window_start_idx < ras->ras_stride_offset)
1115 ras_stride_reset(ras);
1119 * Reset both stride window and normal RA
1122 ras_reset(ras, index);
1123 /* ras->ras_consecutive_pages++; */
1124 ras->ras_consecutive_bytes = 0;
1125 ras_stride_reset(ras);
1126 GOTO(out_unlock, 0);
1129 ras_set_start(ras, index);
1131 if (stride_io_mode(ras)) {
1132 /* Since stride readahead is sentivite to the offset
1133 * of read-ahead, so we use original offset here,
1134 * instead of ras_window_start_idx, which is RPC aligned.
1136 ras->ras_next_readahead_idx = max(index + 1,
1137 ras->ras_next_readahead_idx);
1138 ras->ras_window_start_idx =
1139 max_t(pgoff_t, ras->ras_window_start_idx,
1140 ras->ras_stride_offset >> PAGE_SHIFT);
1142 if (ras->ras_next_readahead_idx < ras->ras_window_start_idx)
1143 ras->ras_next_readahead_idx = ras->ras_window_start_idx;
1145 ras->ras_next_readahead_idx = index + 1;
1148 if (ras->ras_need_increase_window) {
1149 ras_increase_window(inode, ras, ra);
1150 ras->ras_need_increase_window = false;
1155 spin_unlock(&ras->ras_lock);
1158 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1160 struct inode *inode = vmpage->mapping->host;
1161 struct ll_inode_info *lli = ll_i2info(inode);
1164 struct cl_page *page;
1165 struct cl_object *clob;
1166 bool redirtied = false;
1167 bool unlocked = false;
1172 LASSERT(PageLocked(vmpage));
1173 LASSERT(!PageWriteback(vmpage));
1175 LASSERT(ll_i2dtexp(inode) != NULL);
1177 env = cl_env_get(&refcheck);
1179 GOTO(out, result = PTR_ERR(env));
1181 clob = ll_i2info(inode)->lli_clob;
1182 LASSERT(clob != NULL);
1184 io = vvp_env_thread_io(env);
1186 io->ci_ignore_layout = 1;
1187 result = cl_io_init(env, io, CIT_MISC, clob);
1189 page = cl_page_find(env, clob, vmpage->index,
1190 vmpage, CPT_CACHEABLE);
1191 if (!IS_ERR(page)) {
1192 lu_ref_add(&page->cp_reference, "writepage",
1194 cl_page_assume(env, io, page);
1195 result = cl_page_flush(env, io, page);
1198 * Re-dirty page on error so it retries write,
1199 * but not in case when IO has actually
1200 * occurred and completed with an error.
1202 if (!PageError(vmpage)) {
1203 redirty_page_for_writepage(wbc, vmpage);
1208 cl_page_disown(env, io, page);
1210 lu_ref_del(&page->cp_reference,
1211 "writepage", current);
1212 cl_page_put(env, page);
1214 result = PTR_ERR(page);
1217 cl_io_fini(env, io);
1219 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1220 loff_t offset = cl_offset(clob, vmpage->index);
1222 /* Flush page failed because the extent is being written out.
1223 * Wait for the write of extent to be finished to avoid
1224 * breaking kernel which assumes ->writepage should mark
1225 * PageWriteback or clean the page. */
1226 result = cl_sync_file_range(inode, offset,
1227 offset + PAGE_SIZE - 1,
1230 /* actually we may have written more than one page.
1231 * decreasing this page because the caller will count
1233 wbc->nr_to_write -= result - 1;
1238 cl_env_put(env, &refcheck);
1243 if (!lli->lli_async_rc)
1244 lli->lli_async_rc = result;
1245 SetPageError(vmpage);
1247 unlock_page(vmpage);
1252 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1254 struct inode *inode = mapping->host;
1257 enum cl_fsync_mode mode;
1258 int range_whole = 0;
1262 if (wbc->range_cyclic) {
1263 start = (loff_t)mapping->writeback_index << PAGE_SHIFT;
1264 end = OBD_OBJECT_EOF;
1266 start = wbc->range_start;
1267 end = wbc->range_end;
1268 if (end == LLONG_MAX) {
1269 end = OBD_OBJECT_EOF;
1270 range_whole = start == 0;
1274 mode = CL_FSYNC_NONE;
1275 if (wbc->sync_mode == WB_SYNC_ALL)
1276 mode = CL_FSYNC_LOCAL;
1278 if (ll_i2info(inode)->lli_clob == NULL)
1281 /* for directio, it would call writepages() to evict cached pages
1282 * inside the IO context of write, which will cause deadlock at
1283 * layout_conf since it waits for active IOs to complete. */
1284 result = cl_sync_file_range(inode, start, end, mode, 1);
1286 wbc->nr_to_write -= result;
1290 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1291 if (end == OBD_OBJECT_EOF)
1292 mapping->writeback_index = 0;
1294 mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
1299 struct ll_cl_context *ll_cl_find(struct file *file)
1301 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1302 struct ll_cl_context *lcc;
1303 struct ll_cl_context *found = NULL;
1305 read_lock(&fd->fd_lock);
1306 list_for_each_entry(lcc, &fd->fd_lccs, lcc_list) {
1307 if (lcc->lcc_cookie == current) {
1312 read_unlock(&fd->fd_lock);
1317 void ll_cl_add(struct file *file, const struct lu_env *env, struct cl_io *io,
1320 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1321 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1323 memset(lcc, 0, sizeof(*lcc));
1324 INIT_LIST_HEAD(&lcc->lcc_list);
1325 lcc->lcc_cookie = current;
1328 lcc->lcc_type = type;
1330 write_lock(&fd->fd_lock);
1331 list_add(&lcc->lcc_list, &fd->fd_lccs);
1332 write_unlock(&fd->fd_lock);
1335 void ll_cl_remove(struct file *file, const struct lu_env *env)
1337 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1338 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1340 write_lock(&fd->fd_lock);
1341 list_del_init(&lcc->lcc_list);
1342 write_unlock(&fd->fd_lock);
1345 int ll_io_read_page(const struct lu_env *env, struct cl_io *io,
1346 struct cl_page *page, struct file *file)
1348 struct inode *inode = vvp_object_inode(page->cp_obj);
1349 struct ll_sb_info *sbi = ll_i2sbi(inode);
1350 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1351 struct ll_readahead_state *ras = &fd->fd_ras;
1352 struct cl_2queue *queue = &io->ci_queue;
1353 struct cl_sync_io *anchor = NULL;
1354 struct vvp_page *vpg;
1359 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1360 uptodate = vpg->vpg_defer_uptodate;
1362 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1363 sbi->ll_ra_info.ra_max_pages > 0 &&
1364 !vpg->vpg_ra_updated) {
1365 struct vvp_io *vio = vvp_env_io(env);
1366 enum ras_update_flags flags = 0;
1369 flags |= LL_RAS_HIT;
1370 if (!vio->vui_ra_valid)
1371 flags |= LL_RAS_MMAP;
1372 ras_update(sbi, inode, ras, vvp_index(vpg), flags);
1375 cl_2queue_init(queue);
1377 vpg->vpg_ra_used = 1;
1378 cl_page_export(env, page, 1);
1379 cl_page_disown(env, io, page);
1381 anchor = &vvp_env_info(env)->vti_anchor;
1382 cl_sync_io_init(anchor, 1);
1383 page->cp_sync_io = anchor;
1385 cl_2queue_add(queue, page);
1388 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1389 sbi->ll_ra_info.ra_max_pages > 0) {
1392 rc2 = ll_readahead(env, io, &queue->c2_qin, ras,
1394 CDEBUG(D_READA, DFID "%d pages read ahead at %lu\n",
1395 PFID(ll_inode2fid(inode)), rc2, vvp_index(vpg));
1398 if (queue->c2_qin.pl_nr > 0) {
1399 int count = queue->c2_qin.pl_nr;
1400 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
1402 task_io_account_read(PAGE_SIZE * count);
1406 if (anchor != NULL && !cl_page_is_owned(page, io)) { /* have sent */
1407 rc = cl_sync_io_wait(env, anchor, 0);
1409 cl_page_assume(env, io, page);
1410 cl_page_list_del(env, &queue->c2_qout, page);
1412 if (!PageUptodate(cl_page_vmpage(page))) {
1413 /* Failed to read a mirror, discard this page so that
1414 * new page can be created with new mirror.
1416 * TODO: this is not needed after page reinit
1417 * route is implemented */
1418 cl_page_discard(env, io, page);
1420 cl_page_disown(env, io, page);
1423 /* TODO: discard all pages until page reinit route is implemented */
1424 cl_page_list_discard(env, io, &queue->c2_qin);
1426 /* Unlock unsent read pages in case of error. */
1427 cl_page_list_disown(env, io, &queue->c2_qin);
1429 cl_2queue_fini(env, queue);
1435 * Possible return value:
1436 * 0 no async readahead triggered and fast read could not be used.
1437 * 1 no async readahead, but fast read could be used.
1438 * 2 async readahead triggered and fast read could be used too.
1441 static int kickoff_async_readahead(struct file *file, unsigned long pages)
1443 struct ll_readahead_work *lrw;
1444 struct inode *inode = file_inode(file);
1445 struct ll_sb_info *sbi = ll_i2sbi(inode);
1446 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1447 struct ll_readahead_state *ras = &fd->fd_ras;
1448 struct ll_ra_info *ra = &sbi->ll_ra_info;
1449 unsigned long throttle;
1450 pgoff_t start_idx = ras_align(ras, ras->ras_next_readahead_idx);
1451 pgoff_t end_idx = start_idx + pages - 1;
1453 throttle = min(ra->ra_async_pages_per_file_threshold,
1454 ra->ra_max_pages_per_file);
1456 * If this is strided i/o or the window is smaller than the
1457 * throttle limit, we do not do async readahead. Otherwise,
1458 * we do async readahead, allowing the user thread to do fast i/o.
1460 if (stride_io_mode(ras) || !throttle ||
1461 ras->ras_window_pages < throttle)
1464 if ((atomic_read(&ra->ra_cur_pages) + pages) > ra->ra_max_pages)
1467 if (ras->ras_async_last_readpage_idx == start_idx)
1470 /* ll_readahead_work_free() free it */
1473 lrw->lrw_file = get_file(file);
1474 lrw->lrw_start_idx = start_idx;
1475 lrw->lrw_end_idx = end_idx;
1476 spin_lock(&ras->ras_lock);
1477 ras->ras_next_readahead_idx = end_idx + 1;
1478 ras->ras_async_last_readpage_idx = start_idx;
1479 spin_unlock(&ras->ras_lock);
1480 ll_readahead_work_add(inode, lrw);
1488 int ll_readpage(struct file *file, struct page *vmpage)
1490 struct inode *inode = file_inode(file);
1491 struct cl_object *clob = ll_i2info(inode)->lli_clob;
1492 struct ll_cl_context *lcc;
1493 const struct lu_env *env = NULL;
1494 struct cl_io *io = NULL;
1495 struct cl_page *page;
1496 struct ll_sb_info *sbi = ll_i2sbi(inode);
1500 lcc = ll_cl_find(file);
1506 if (io == NULL) { /* fast read */
1507 struct inode *inode = file_inode(file);
1508 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1509 struct ll_readahead_state *ras = &fd->fd_ras;
1510 struct lu_env *local_env = NULL;
1511 unsigned long fast_read_pages =
1512 max(RA_REMAIN_WINDOW_MIN, ras->ras_rpc_pages);
1513 struct vvp_page *vpg;
1517 /* TODO: need to verify the layout version to make sure
1518 * the page is not invalid due to layout change. */
1519 page = cl_vmpage_page(vmpage, clob);
1521 unlock_page(vmpage);
1522 ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1526 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1527 if (vpg->vpg_defer_uptodate) {
1528 enum ras_update_flags flags = LL_RAS_HIT;
1530 if (lcc && lcc->lcc_type == LCC_MMAP)
1531 flags |= LL_RAS_MMAP;
1533 /* For fast read, it updates read ahead state only
1534 * if the page is hit in cache because non cache page
1535 * case will be handled by slow read later. */
1536 ras_update(sbi, inode, ras, vvp_index(vpg), flags);
1537 /* avoid duplicate ras_update() call */
1538 vpg->vpg_ra_updated = 1;
1540 /* Check if we can issue a readahead RPC, if that is
1541 * the case, we can't do fast IO because we will need
1542 * a cl_io to issue the RPC. */
1543 if (ras->ras_window_start_idx + ras->ras_window_pages <
1544 ras->ras_next_readahead_idx + fast_read_pages ||
1545 kickoff_async_readahead(file, fast_read_pages) > 0)
1550 local_env = cl_env_percpu_get();
1554 /* export the page and skip io stack */
1556 vpg->vpg_ra_used = 1;
1557 cl_page_export(env, page, 1);
1559 ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1561 /* release page refcount before unlocking the page to ensure
1562 * the object won't be destroyed in the calling path of
1563 * cl_page_put(). Please see comment in ll_releasepage(). */
1564 cl_page_put(env, page);
1565 unlock_page(vmpage);
1567 cl_env_percpu_put(local_env);
1573 * Direct read can fall back to buffered read, but DIO is done
1574 * with lockless i/o, and buffered requires LDLM locking, so in
1575 * this case we must restart without lockless.
1577 if (file->f_flags & O_DIRECT &&
1578 lcc && lcc->lcc_type == LCC_RW &&
1579 !io->ci_ignore_lockless) {
1580 unlock_page(vmpage);
1581 io->ci_ignore_lockless = 1;
1582 io->ci_need_restart = 1;
1586 LASSERT(io->ci_state == CIS_IO_GOING);
1587 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
1588 if (!IS_ERR(page)) {
1589 LASSERT(page->cp_type == CPT_CACHEABLE);
1590 if (likely(!PageUptodate(vmpage))) {
1591 cl_page_assume(env, io, page);
1593 result = ll_io_read_page(env, io, page, file);
1595 /* Page from a non-object file. */
1596 unlock_page(vmpage);
1599 cl_page_put(env, page);
1601 unlock_page(vmpage);
1602 result = PTR_ERR(page);