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,
85 unsigned long pages, unsigned long min)
87 struct ll_ra_info *ra = &sbi->ll_ra_info;
91 /* If read-ahead pages left are less than 1M, do not do read-ahead,
92 * otherwise it will form small read RPC(< 1M), which hurt server
93 * performance a lot. */
94 ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages),
96 if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages))
99 if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
100 atomic_sub(ret, &ra->ra_cur_pages);
106 /* override ra limit for maximum performance */
107 atomic_add(min - ret, &ra->ra_cur_pages);
113 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
115 struct ll_ra_info *ra = &sbi->ll_ra_info;
116 atomic_sub(len, &ra->ra_cur_pages);
119 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
121 LASSERTF(which < _NR_RA_STAT, "which: %u\n", which);
122 lprocfs_counter_incr(sbi->ll_ra_stats, which);
125 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
127 struct ll_sb_info *sbi = ll_i2sbi(inode);
128 ll_ra_stats_inc_sbi(sbi, which);
131 #define RAS_CDEBUG(ras) \
133 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu rpc %lu " \
134 "r %lu ri %lu csr %lu sf %lu sp %lu sl %lu lr %lu\n", \
135 ras->ras_last_readpage, ras->ras_consecutive_requests, \
136 ras->ras_consecutive_pages, ras->ras_window_start, \
137 ras->ras_window_len, ras->ras_next_readahead, \
139 ras->ras_requests, ras->ras_request_index, \
140 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
141 ras->ras_stride_pages, ras->ras_stride_length, \
142 ras->ras_async_last_readpage)
144 static int index_in_window(unsigned long index, unsigned long point,
145 unsigned long before, unsigned long after)
147 unsigned long start = point - before, end = point + after;
154 return start <= index && index <= end;
157 void ll_ras_enter(struct file *f)
159 struct ll_file_data *fd = LUSTRE_FPRIVATE(f);
160 struct ll_readahead_state *ras = &fd->fd_ras;
162 spin_lock(&ras->ras_lock);
164 ras->ras_request_index = 0;
165 ras->ras_consecutive_requests++;
166 spin_unlock(&ras->ras_lock);
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)
180 struct cl_object *clob = io->ci_obj;
181 struct inode *inode = vvp_object_inode(clob);
183 struct cl_page *page;
184 struct vvp_page *vpg;
185 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
187 const char *msg = NULL;
190 vmpage = grab_cache_page_nowait(inode->i_mapping, index);
191 if (vmpage == NULL) {
192 which = RA_STAT_FAILED_GRAB_PAGE;
193 msg = "g_c_p_n failed";
194 GOTO(out, rc = -EBUSY);
197 /* Check if vmpage was truncated or reclaimed */
198 if (vmpage->mapping != inode->i_mapping) {
199 which = RA_STAT_WRONG_GRAB_PAGE;
200 msg = "g_c_p_n returned invalid page";
201 GOTO(out, rc = -EBUSY);
204 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
206 which = RA_STAT_FAILED_GRAB_PAGE;
207 msg = "cl_page_find failed";
208 GOTO(out, rc = PTR_ERR(page));
211 lu_ref_add(&page->cp_reference, "ra", current);
212 cl_page_assume(env, io, page);
213 vpg = cl2vvp_page(cl_object_page_slice(clob, page));
214 if (!vpg->vpg_defer_uptodate && !PageUptodate(vmpage)) {
215 vpg->vpg_defer_uptodate = 1;
216 vpg->vpg_ra_used = 0;
217 cl_page_list_add(queue, page);
219 /* skip completed pages */
220 cl_page_unassume(env, io, page);
221 /* This page is already uptodate, returning a positive number
222 * to tell the callers about this */
226 lu_ref_del(&page->cp_reference, "ra", current);
227 cl_page_put(env, page);
230 if (vmpage != NULL) {
236 ll_ra_stats_inc(inode, which);
237 CDEBUG(D_READA, "%s\n", msg);
244 #define RIA_DEBUG(ria) \
245 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
246 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
249 static inline int stride_io_mode(struct ll_readahead_state *ras)
251 return ras->ras_consecutive_stride_requests > 1;
254 /* The function calculates how much pages will be read in
255 * [off, off + length], in such stride IO area,
256 * stride_offset = st_off, stride_lengh = st_len,
257 * stride_pages = st_pgs
259 * |------------------|*****|------------------|*****|------------|*****|....
262 * |----- st_len -----|
264 * How many pages it should read in such pattern
265 * |-------------------------------------------------------------|
267 * |<------ length ------->|
269 * = |<----->| + |-------------------------------------| + |---|
270 * start_left st_pgs * i end_left
273 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
274 unsigned long off, unsigned long length)
276 __u64 start = off > st_off ? off - st_off : 0;
277 __u64 end = off + length > st_off ? off + length - st_off : 0;
278 unsigned long start_left = 0;
279 unsigned long end_left = 0;
280 unsigned long pg_count;
282 if (st_len == 0 || length == 0 || end == 0)
285 start_left = do_div(start, st_len);
286 if (start_left < st_pgs)
287 start_left = st_pgs - start_left;
291 end_left = do_div(end, st_len);
292 if (end_left > st_pgs)
295 CDEBUG(D_READA, "start %llu, end %llu start_left %lu end_left %lu\n",
296 start, end, start_left, end_left);
299 pg_count = end_left - (st_pgs - start_left);
301 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
303 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
304 "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
309 static int ria_page_count(struct ra_io_arg *ria)
311 __u64 length = ria->ria_end >= ria->ria_start ?
312 ria->ria_end - ria->ria_start + 1 : 0;
314 return stride_pg_count(ria->ria_stoff, ria->ria_length,
315 ria->ria_pages, ria->ria_start,
319 static unsigned long ras_align(struct ll_readahead_state *ras,
321 unsigned long *remainder)
323 unsigned long rem = index % ras->ras_rpc_size;
324 if (remainder != NULL)
329 /*Check whether the index is in the defined ra-window */
330 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
332 /* If ria_length == ria_pages, it means non-stride I/O mode,
333 * idx should always inside read-ahead window in this case
334 * For stride I/O mode, just check whether the idx is inside
336 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
337 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
338 ria->ria_length < ria->ria_pages);
342 ll_read_ahead_pages(const struct lu_env *env, struct cl_io *io,
343 struct cl_page_list *queue, struct ll_readahead_state *ras,
344 struct ra_io_arg *ria, pgoff_t *ra_end)
346 struct cl_read_ahead ra = { 0 };
347 int rc = 0, count = 0;
351 LASSERT(ria != NULL);
354 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
355 for (page_idx = ria->ria_start;
356 page_idx <= ria->ria_end && ria->ria_reserved > 0; page_idx++) {
357 if (ras_inside_ra_window(page_idx, ria)) {
358 if (ra.cra_end == 0 || ra.cra_end < page_idx) {
361 cl_read_ahead_release(env, &ra);
363 rc = cl_io_read_ahead(env, io, page_idx, &ra);
367 CDEBUG(D_READA, "idx: %lu, ra: %lu, rpc: %lu\n",
368 page_idx, ra.cra_end, ra.cra_rpc_size);
369 LASSERTF(ra.cra_end >= page_idx,
370 "object: %p, indcies %lu / %lu\n",
371 io->ci_obj, ra.cra_end, page_idx);
372 /* update read ahead RPC size.
373 * NB: it's racy but doesn't matter */
374 if (ras->ras_rpc_size != ra.cra_rpc_size &&
376 ras->ras_rpc_size = ra.cra_rpc_size;
377 /* trim it to align with optimal RPC size */
378 end = ras_align(ras, ria->ria_end + 1, NULL);
379 if (end > 0 && !ria->ria_eof)
380 ria->ria_end = end - 1;
381 if (ria->ria_end < ria->ria_end_min)
382 ria->ria_end = ria->ria_end_min;
383 if (ria->ria_end > ra.cra_end)
384 ria->ria_end = ra.cra_end;
386 if (page_idx > ria->ria_end)
389 /* If the page is inside the read-ahead window */
390 rc = ll_read_ahead_page(env, io, queue, page_idx);
395 /* Only subtract from reserve & count the page if we
396 * really did readahead on that page. */
401 } else if (stride_ria) {
402 /* If it is not in the read-ahead window, and it is
403 * read-ahead mode, then check whether it should skip
406 /* FIXME: This assertion only is valid when it is for
407 * forward read-ahead, it will be fixed when backward
408 * read-ahead is implemented */
409 LASSERTF(page_idx >= ria->ria_stoff,
410 "Invalid page_idx %lu rs %lu re %lu ro %lu "
411 "rl %lu rp %lu\n", page_idx,
412 ria->ria_start, ria->ria_end, ria->ria_stoff,
413 ria->ria_length, ria->ria_pages);
414 offset = page_idx - ria->ria_stoff;
415 offset = offset % (ria->ria_length);
416 if (offset > ria->ria_pages) {
417 page_idx += ria->ria_length - offset;
418 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
419 ria->ria_length - offset);
425 cl_read_ahead_release(env, &ra);
430 static void ll_readahead_work_free(struct ll_readahead_work *work)
432 fput(work->lrw_file);
436 static void ll_readahead_handle_work(struct work_struct *wq);
437 static void ll_readahead_work_add(struct inode *inode,
438 struct ll_readahead_work *work)
440 INIT_WORK(&work->lrw_readahead_work, ll_readahead_handle_work);
441 queue_work(ll_i2sbi(inode)->ll_ra_info.ll_readahead_wq,
442 &work->lrw_readahead_work);
445 static int ll_readahead_file_kms(const struct lu_env *env,
446 struct cl_io *io, __u64 *kms)
448 struct cl_object *clob;
450 struct cl_attr *attr = vvp_env_thread_attr(env);
454 inode = vvp_object_inode(clob);
456 cl_object_attr_lock(clob);
457 ret = cl_object_attr_get(env, clob, attr);
458 cl_object_attr_unlock(clob);
463 *kms = attr->cat_kms;
467 static void ll_readahead_handle_work(struct work_struct *wq)
469 struct ll_readahead_work *work;
472 struct ra_io_arg *ria;
474 struct ll_file_data *fd;
475 struct ll_readahead_state *ras;
477 struct cl_2queue *queue;
479 unsigned long len, mlen = 0;
483 unsigned long end_index;
485 work = container_of(wq, struct ll_readahead_work,
487 fd = LUSTRE_FPRIVATE(work->lrw_file);
489 file = work->lrw_file;
490 inode = file_inode(file);
492 env = cl_env_alloc(&refcheck, LCT_NOREF);
494 GOTO(out_free_work, rc = PTR_ERR(env));
496 io = vvp_env_thread_io(env);
497 ll_io_init(io, file, CIT_READ);
499 rc = ll_readahead_file_kms(env, io, &kms);
501 GOTO(out_put_env, rc);
504 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
505 GOTO(out_put_env, rc = 0);
508 ria = &ll_env_info(env)->lti_ria;
509 memset(ria, 0, sizeof(*ria));
511 ria->ria_start = work->lrw_start;
512 /* Truncate RA window to end of file */
513 end_index = (unsigned long)((kms - 1) >> PAGE_SHIFT);
514 if (end_index <= work->lrw_end) {
515 work->lrw_end = end_index;
518 if (work->lrw_end <= work->lrw_start)
519 GOTO(out_put_env, rc = 0);
521 ria->ria_end = work->lrw_end;
522 len = ria->ria_end - ria->ria_start + 1;
523 ria->ria_reserved = ll_ra_count_get(ll_i2sbi(inode), ria,
524 ria_page_count(ria), mlen);
527 "async reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
528 ria->ria_reserved, len, mlen,
529 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
530 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
532 if (ria->ria_reserved < len) {
533 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
534 if (PAGES_TO_MiB(ria->ria_reserved) < 1) {
535 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
536 GOTO(out_put_env, rc = 0);
540 rc = cl_io_rw_init(env, io, CIT_READ, ria->ria_start, len);
542 GOTO(out_put_env, rc);
544 vvp_env_io(env)->vui_io_subtype = IO_NORMAL;
545 vvp_env_io(env)->vui_fd = fd;
546 io->ci_state = CIS_LOCKED;
547 io->ci_async_readahead = true;
548 rc = cl_io_start(env, io);
550 GOTO(out_io_fini, rc);
552 queue = &io->ci_queue;
553 cl_2queue_init(queue);
555 rc = ll_read_ahead_pages(env, io, &queue->c2_qin, ras, ria, &ra_end);
556 if (ria->ria_reserved != 0)
557 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
558 if (queue->c2_qin.pl_nr > 0) {
559 int count = queue->c2_qin.pl_nr;
561 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
563 task_io_account_read(PAGE_SIZE * count);
565 if (ria->ria_end == ra_end && ra_end == (kms >> PAGE_SHIFT))
566 ll_ra_stats_inc(inode, RA_STAT_EOF);
568 if (ra_end != ria->ria_end)
569 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
571 /* TODO: discard all pages until page reinit route is implemented */
572 cl_page_list_discard(env, io, &queue->c2_qin);
574 /* Unlock unsent read pages in case of error. */
575 cl_page_list_disown(env, io, &queue->c2_qin);
577 cl_2queue_fini(env, queue);
582 cl_env_put(env, &refcheck);
585 ll_ra_stats_inc_sbi(ll_i2sbi(inode), RA_STAT_ASYNC);
586 ll_readahead_work_free(work);
589 static int ll_readahead(const struct lu_env *env, struct cl_io *io,
590 struct cl_page_list *queue,
591 struct ll_readahead_state *ras, bool hit,
594 struct vvp_io *vio = vvp_env_io(env);
595 struct ll_thread_info *lti = ll_env_info(env);
596 unsigned long len, mlen = 0;
597 pgoff_t ra_end = 0, start = 0, end = 0;
599 struct ra_io_arg *ria = <i->lti_ria;
600 struct cl_object *clob;
606 inode = vvp_object_inode(clob);
608 memset(ria, 0, sizeof *ria);
609 ret = ll_readahead_file_kms(env, io, &kms);
614 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
618 spin_lock(&ras->ras_lock);
621 * Note: other thread might rollback the ras_next_readahead,
622 * if it can not get the full size of prepared pages, see the
623 * end of this function. For stride read ahead, it needs to
624 * make sure the offset is no less than ras_stride_offset,
625 * so that stride read ahead can work correctly.
627 if (stride_io_mode(ras))
628 start = max(ras->ras_next_readahead, ras->ras_stride_offset);
630 start = ras->ras_next_readahead;
632 if (ras->ras_window_len > 0)
633 end = ras->ras_window_start + ras->ras_window_len - 1;
635 /* Enlarge the RA window to encompass the full read */
636 if (vio->vui_ra_valid &&
637 end < vio->vui_ra_start + vio->vui_ra_count - 1)
638 end = vio->vui_ra_start + vio->vui_ra_count - 1;
641 unsigned long end_index;
643 /* Truncate RA window to end of file */
644 end_index = (unsigned long)((kms - 1) >> PAGE_SHIFT);
645 if (end_index <= end) {
650 ria->ria_start = start;
652 /* If stride I/O mode is detected, get stride window*/
653 if (stride_io_mode(ras)) {
654 ria->ria_stoff = ras->ras_stride_offset;
655 ria->ria_length = ras->ras_stride_length;
656 ria->ria_pages = ras->ras_stride_pages;
658 spin_unlock(&ras->ras_lock);
661 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
664 len = ria_page_count(ria);
666 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
671 CDEBUG(D_READA, DFID": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
672 PFID(lu_object_fid(&clob->co_lu)),
673 ria->ria_start, ria->ria_end,
674 vio->vui_ra_valid ? vio->vui_ra_start : 0,
675 vio->vui_ra_valid ? vio->vui_ra_count : 0,
678 /* at least to extend the readahead window to cover current read */
679 if (!hit && vio->vui_ra_valid &&
680 vio->vui_ra_start + vio->vui_ra_count > ria->ria_start) {
681 unsigned long remainder;
683 /* to the end of current read window. */
684 mlen = vio->vui_ra_start + vio->vui_ra_count - ria->ria_start;
685 /* trim to RPC boundary */
686 ras_align(ras, ria->ria_start, &remainder);
687 mlen = min(mlen, ras->ras_rpc_size - remainder);
688 ria->ria_end_min = ria->ria_start + mlen;
691 ria->ria_reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len, mlen);
692 if (ria->ria_reserved < len)
693 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
695 CDEBUG(D_READA, "reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
696 ria->ria_reserved, len, mlen,
697 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
698 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
700 ret = ll_read_ahead_pages(env, io, queue, ras, ria, &ra_end);
702 if (ria->ria_reserved != 0)
703 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
705 if (ra_end == end && ra_end == (kms >> PAGE_SHIFT))
706 ll_ra_stats_inc(inode, RA_STAT_EOF);
708 CDEBUG(D_READA, "ra_end = %lu end = %lu stride end = %lu pages = %d\n",
709 ra_end, end, ria->ria_end, ret);
712 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
714 /* update the ras so that the next read-ahead tries from
715 * where we left off. */
716 spin_lock(&ras->ras_lock);
717 ras->ras_next_readahead = ra_end + 1;
718 spin_unlock(&ras->ras_lock);
725 static void ras_set_start(struct inode *inode, struct ll_readahead_state *ras,
728 ras->ras_window_start = ras_align(ras, index, NULL);
731 /* called with the ras_lock held or from places where it doesn't matter */
732 static void ras_reset(struct inode *inode, struct ll_readahead_state *ras,
735 ras->ras_last_readpage = index;
736 ras->ras_consecutive_requests = 0;
737 ras->ras_consecutive_pages = 0;
738 ras->ras_window_len = 0;
739 ras_set_start(inode, ras, index);
740 ras->ras_next_readahead = max(ras->ras_window_start, index + 1);
745 /* called with the ras_lock held or from places where it doesn't matter */
746 static void ras_stride_reset(struct ll_readahead_state *ras)
748 ras->ras_consecutive_stride_requests = 0;
749 ras->ras_stride_length = 0;
750 ras->ras_stride_pages = 0;
754 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
756 spin_lock_init(&ras->ras_lock);
757 ras->ras_rpc_size = PTLRPC_MAX_BRW_PAGES;
758 ras_reset(inode, ras, 0);
759 ras->ras_requests = 0;
763 * Check whether the read request is in the stride window.
764 * If it is in the stride window, return 1, otherwise return 0.
766 static int index_in_stride_window(struct ll_readahead_state *ras,
769 unsigned long stride_gap;
771 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
772 ras->ras_stride_pages == ras->ras_stride_length)
775 stride_gap = index - ras->ras_last_readpage - 1;
777 /* If it is contiguous read */
779 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
781 /* Otherwise check the stride by itself */
782 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
783 ras->ras_consecutive_pages == ras->ras_stride_pages;
786 static void ras_update_stride_detector(struct ll_readahead_state *ras,
789 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
791 if (!stride_io_mode(ras) && (stride_gap != 0 ||
792 ras->ras_consecutive_stride_requests == 0)) {
793 ras->ras_stride_pages = ras->ras_consecutive_pages;
794 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
796 LASSERT(ras->ras_request_index == 0);
797 LASSERT(ras->ras_consecutive_stride_requests == 0);
799 if (index <= ras->ras_last_readpage) {
800 /*Reset stride window for forward read*/
801 ras_stride_reset(ras);
805 ras->ras_stride_pages = ras->ras_consecutive_pages;
806 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
813 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
815 return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
816 ras->ras_stride_pages, ras->ras_stride_offset,
820 /* Stride Read-ahead window will be increased inc_len according to
821 * stride I/O pattern */
822 static void ras_stride_increase_window(struct ll_readahead_state *ras,
823 struct ll_ra_info *ra,
824 unsigned long inc_len)
826 unsigned long left, step, window_len;
827 unsigned long stride_len;
829 LASSERT(ras->ras_stride_length > 0);
830 LASSERTF(ras->ras_window_start + ras->ras_window_len
831 >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
832 " stride_offset %lu\n", ras->ras_window_start,
833 ras->ras_window_len, ras->ras_stride_offset);
835 stride_len = ras->ras_window_start + ras->ras_window_len -
836 ras->ras_stride_offset;
838 left = stride_len % ras->ras_stride_length;
839 window_len = ras->ras_window_len - left;
841 if (left < ras->ras_stride_pages)
844 left = ras->ras_stride_pages + inc_len;
846 LASSERT(ras->ras_stride_pages != 0);
848 step = left / ras->ras_stride_pages;
849 left %= ras->ras_stride_pages;
851 window_len += step * ras->ras_stride_length + left;
853 if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
854 ras->ras_window_len = window_len;
859 static void ras_increase_window(struct inode *inode,
860 struct ll_readahead_state *ras,
861 struct ll_ra_info *ra)
863 /* The stretch of ra-window should be aligned with max rpc_size
864 * but current clio architecture does not support retrieve such
865 * information from lower layer. FIXME later
867 if (stride_io_mode(ras)) {
868 ras_stride_increase_window(ras, ra, ras->ras_rpc_size);
872 wlen = min(ras->ras_window_len + ras->ras_rpc_size,
873 ra->ra_max_pages_per_file);
874 if (wlen < ras->ras_rpc_size)
875 ras->ras_window_len = wlen;
877 ras->ras_window_len = ras_align(ras, wlen, NULL);
881 static void ras_update(struct ll_sb_info *sbi, struct inode *inode,
882 struct ll_readahead_state *ras, unsigned long index,
883 enum ras_update_flags flags)
885 struct ll_ra_info *ra = &sbi->ll_ra_info;
886 bool hit = flags & LL_RAS_HIT;
887 int zero = 0, stride_detect = 0, ra_miss = 0;
890 spin_lock(&ras->ras_lock);
893 CDEBUG(D_READA, DFID " pages at %lu miss.\n",
894 PFID(ll_inode2fid(inode)), index);
895 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
897 /* reset the read-ahead window in two cases. First when the app seeks
898 * or reads to some other part of the file. Secondly if we get a
899 * read-ahead miss that we think we've previously issued. This can
900 * be a symptom of there being so many read-ahead pages that the VM is
901 * reclaiming it before we get to it. */
902 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
904 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
905 } else if (!hit && ras->ras_window_len &&
906 index < ras->ras_next_readahead &&
907 index_in_window(index, ras->ras_window_start, 0,
908 ras->ras_window_len)) {
910 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
913 /* On the second access to a file smaller than the tunable
914 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
915 * file up to ra_max_pages_per_file. This is simply a best effort
916 * and only occurs once per open file. Normal RA behavior is reverted
917 * to for subsequent IO. The mmap case does not increment
918 * ras_requests and thus can never trigger this behavior. */
919 if (ras->ras_requests >= 2 && !ras->ras_request_index) {
922 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
925 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
926 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
929 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
930 ras->ras_window_start = 0;
931 ras->ras_next_readahead = index + 1;
932 ras->ras_window_len = min(ra->ra_max_pages_per_file,
933 ra->ra_max_read_ahead_whole_pages);
938 /* check whether it is in stride I/O mode*/
939 if (!index_in_stride_window(ras, index)) {
940 if (ras->ras_consecutive_stride_requests == 0 &&
941 ras->ras_request_index == 0) {
942 ras_update_stride_detector(ras, index);
943 ras->ras_consecutive_stride_requests++;
945 ras_stride_reset(ras);
947 ras_reset(inode, ras, index);
948 ras->ras_consecutive_pages++;
951 ras->ras_consecutive_pages = 0;
952 ras->ras_consecutive_requests = 0;
953 if (++ras->ras_consecutive_stride_requests > 1)
959 if (index_in_stride_window(ras, index) &&
960 stride_io_mode(ras)) {
961 if (index != ras->ras_last_readpage + 1)
962 ras->ras_consecutive_pages = 0;
963 ras_reset(inode, ras, index);
965 /* If stride-RA hit cache miss, the stride
966 * detector will not be reset to avoid the
967 * overhead of redetecting read-ahead mode,
968 * but on the condition that the stride window
969 * is still intersect with normal sequential
970 * read-ahead window. */
971 if (ras->ras_window_start <
972 ras->ras_stride_offset)
973 ras_stride_reset(ras);
976 /* Reset both stride window and normal RA
978 ras_reset(inode, ras, index);
979 ras->ras_consecutive_pages++;
980 ras_stride_reset(ras);
983 } else if (stride_io_mode(ras)) {
984 /* If this is contiguous read but in stride I/O mode
985 * currently, check whether stride step still is valid,
986 * if invalid, it will reset the stride ra window*/
987 if (!index_in_stride_window(ras, index)) {
988 /* Shrink stride read-ahead window to be zero */
989 ras_stride_reset(ras);
990 ras->ras_window_len = 0;
991 ras->ras_next_readahead = index;
995 ras->ras_consecutive_pages++;
996 ras->ras_last_readpage = index;
997 ras_set_start(inode, ras, index);
999 if (stride_io_mode(ras)) {
1000 /* Since stride readahead is sentivite to the offset
1001 * of read-ahead, so we use original offset here,
1002 * instead of ras_window_start, which is RPC aligned */
1003 ras->ras_next_readahead = max(index + 1,
1004 ras->ras_next_readahead);
1005 ras->ras_window_start = max(ras->ras_stride_offset,
1006 ras->ras_window_start);
1008 if (ras->ras_next_readahead < ras->ras_window_start)
1009 ras->ras_next_readahead = ras->ras_window_start;
1011 ras->ras_next_readahead = index + 1;
1015 /* Trigger RA in the mmap case where ras_consecutive_requests
1016 * is not incremented and thus can't be used to trigger RA */
1017 if (ras->ras_consecutive_pages >= 4 && flags & LL_RAS_MMAP) {
1018 ras_increase_window(inode, ras, ra);
1019 /* reset consecutive pages so that the readahead window can
1020 * grow gradually. */
1021 ras->ras_consecutive_pages = 0;
1022 GOTO(out_unlock, 0);
1025 /* Initially reset the stride window offset to next_readahead*/
1026 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
1028 * Once stride IO mode is detected, next_readahead should be
1029 * reset to make sure next_readahead > stride offset
1031 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
1032 ras->ras_stride_offset = index;
1033 ras->ras_window_start = max(index, ras->ras_window_start);
1036 /* The initial ras_window_len is set to the request size. To avoid
1037 * uselessly reading and discarding pages for random IO the window is
1038 * only increased once per consecutive request received. */
1039 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
1040 !ras->ras_request_index)
1041 ras_increase_window(inode, ras, ra);
1045 ras->ras_request_index++;
1046 spin_unlock(&ras->ras_lock);
1050 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1052 struct inode *inode = vmpage->mapping->host;
1053 struct ll_inode_info *lli = ll_i2info(inode);
1056 struct cl_page *page;
1057 struct cl_object *clob;
1058 bool redirtied = false;
1059 bool unlocked = false;
1064 LASSERT(PageLocked(vmpage));
1065 LASSERT(!PageWriteback(vmpage));
1067 LASSERT(ll_i2dtexp(inode) != NULL);
1069 env = cl_env_get(&refcheck);
1071 GOTO(out, result = PTR_ERR(env));
1073 clob = ll_i2info(inode)->lli_clob;
1074 LASSERT(clob != NULL);
1076 io = vvp_env_thread_io(env);
1078 io->ci_ignore_layout = 1;
1079 result = cl_io_init(env, io, CIT_MISC, clob);
1081 page = cl_page_find(env, clob, vmpage->index,
1082 vmpage, CPT_CACHEABLE);
1083 if (!IS_ERR(page)) {
1084 lu_ref_add(&page->cp_reference, "writepage",
1086 cl_page_assume(env, io, page);
1087 result = cl_page_flush(env, io, page);
1090 * Re-dirty page on error so it retries write,
1091 * but not in case when IO has actually
1092 * occurred and completed with an error.
1094 if (!PageError(vmpage)) {
1095 redirty_page_for_writepage(wbc, vmpage);
1100 cl_page_disown(env, io, page);
1102 lu_ref_del(&page->cp_reference,
1103 "writepage", current);
1104 cl_page_put(env, page);
1106 result = PTR_ERR(page);
1109 cl_io_fini(env, io);
1111 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1112 loff_t offset = cl_offset(clob, vmpage->index);
1114 /* Flush page failed because the extent is being written out.
1115 * Wait for the write of extent to be finished to avoid
1116 * breaking kernel which assumes ->writepage should mark
1117 * PageWriteback or clean the page. */
1118 result = cl_sync_file_range(inode, offset,
1119 offset + PAGE_SIZE - 1,
1122 /* actually we may have written more than one page.
1123 * decreasing this page because the caller will count
1125 wbc->nr_to_write -= result - 1;
1130 cl_env_put(env, &refcheck);
1135 if (!lli->lli_async_rc)
1136 lli->lli_async_rc = result;
1137 SetPageError(vmpage);
1139 unlock_page(vmpage);
1144 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1146 struct inode *inode = mapping->host;
1149 enum cl_fsync_mode mode;
1150 int range_whole = 0;
1154 if (wbc->range_cyclic) {
1155 start = mapping->writeback_index << PAGE_SHIFT;
1156 end = OBD_OBJECT_EOF;
1158 start = wbc->range_start;
1159 end = wbc->range_end;
1160 if (end == LLONG_MAX) {
1161 end = OBD_OBJECT_EOF;
1162 range_whole = start == 0;
1166 mode = CL_FSYNC_NONE;
1167 if (wbc->sync_mode == WB_SYNC_ALL)
1168 mode = CL_FSYNC_LOCAL;
1170 if (ll_i2info(inode)->lli_clob == NULL)
1173 /* for directio, it would call writepages() to evict cached pages
1174 * inside the IO context of write, which will cause deadlock at
1175 * layout_conf since it waits for active IOs to complete. */
1176 result = cl_sync_file_range(inode, start, end, mode, 1);
1178 wbc->nr_to_write -= result;
1182 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1183 if (end == OBD_OBJECT_EOF)
1184 mapping->writeback_index = 0;
1186 mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
1191 struct ll_cl_context *ll_cl_find(struct file *file)
1193 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1194 struct ll_cl_context *lcc;
1195 struct ll_cl_context *found = NULL;
1197 read_lock(&fd->fd_lock);
1198 list_for_each_entry(lcc, &fd->fd_lccs, lcc_list) {
1199 if (lcc->lcc_cookie == current) {
1204 read_unlock(&fd->fd_lock);
1209 void ll_cl_add(struct file *file, const struct lu_env *env, struct cl_io *io,
1212 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1213 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1215 memset(lcc, 0, sizeof(*lcc));
1216 INIT_LIST_HEAD(&lcc->lcc_list);
1217 lcc->lcc_cookie = current;
1220 lcc->lcc_type = type;
1222 write_lock(&fd->fd_lock);
1223 list_add(&lcc->lcc_list, &fd->fd_lccs);
1224 write_unlock(&fd->fd_lock);
1227 void ll_cl_remove(struct file *file, const struct lu_env *env)
1229 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1230 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1232 write_lock(&fd->fd_lock);
1233 list_del_init(&lcc->lcc_list);
1234 write_unlock(&fd->fd_lock);
1237 int ll_io_read_page(const struct lu_env *env, struct cl_io *io,
1238 struct cl_page *page, struct file *file)
1240 struct inode *inode = vvp_object_inode(page->cp_obj);
1241 struct ll_sb_info *sbi = ll_i2sbi(inode);
1242 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1243 struct ll_readahead_state *ras = &fd->fd_ras;
1244 struct cl_2queue *queue = &io->ci_queue;
1245 struct cl_sync_io *anchor = NULL;
1246 struct vvp_page *vpg;
1251 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1252 uptodate = vpg->vpg_defer_uptodate;
1254 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1255 sbi->ll_ra_info.ra_max_pages > 0 &&
1256 !vpg->vpg_ra_updated) {
1257 struct vvp_io *vio = vvp_env_io(env);
1258 enum ras_update_flags flags = 0;
1261 flags |= LL_RAS_HIT;
1262 if (!vio->vui_ra_valid)
1263 flags |= LL_RAS_MMAP;
1264 ras_update(sbi, inode, ras, vvp_index(vpg), flags);
1267 cl_2queue_init(queue);
1269 vpg->vpg_ra_used = 1;
1270 cl_page_export(env, page, 1);
1271 cl_page_disown(env, io, page);
1273 anchor = &vvp_env_info(env)->vti_anchor;
1274 cl_sync_io_init(anchor, 1, &cl_sync_io_end);
1275 page->cp_sync_io = anchor;
1277 cl_2queue_add(queue, page);
1280 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1281 sbi->ll_ra_info.ra_max_pages > 0) {
1284 rc2 = ll_readahead(env, io, &queue->c2_qin, ras,
1286 CDEBUG(D_READA, DFID "%d pages read ahead at %lu\n",
1287 PFID(ll_inode2fid(inode)), rc2, vvp_index(vpg));
1290 if (queue->c2_qin.pl_nr > 0) {
1291 int count = queue->c2_qin.pl_nr;
1292 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
1294 task_io_account_read(PAGE_SIZE * count);
1298 if (anchor != NULL && !cl_page_is_owned(page, io)) { /* have sent */
1299 rc = cl_sync_io_wait(env, anchor, 0);
1301 cl_page_assume(env, io, page);
1302 cl_page_list_del(env, &queue->c2_qout, page);
1304 if (!PageUptodate(cl_page_vmpage(page))) {
1305 /* Failed to read a mirror, discard this page so that
1306 * new page can be created with new mirror.
1308 * TODO: this is not needed after page reinit
1309 * route is implemented */
1310 cl_page_discard(env, io, page);
1312 cl_page_disown(env, io, page);
1315 /* TODO: discard all pages until page reinit route is implemented */
1316 cl_page_list_discard(env, io, &queue->c2_qin);
1318 /* Unlock unsent read pages in case of error. */
1319 cl_page_list_disown(env, io, &queue->c2_qin);
1321 cl_2queue_fini(env, queue);
1327 * Possible return value:
1328 * 0 no async readahead triggered and fast read could not be used.
1329 * 1 no async readahead, but fast read could be used.
1330 * 2 async readahead triggered and fast read could be used too.
1333 static int kickoff_async_readahead(struct file *file, unsigned long pages)
1335 struct ll_readahead_work *lrw;
1336 struct inode *inode = file_inode(file);
1337 struct ll_sb_info *sbi = ll_i2sbi(inode);
1338 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1339 struct ll_readahead_state *ras = &fd->fd_ras;
1340 struct ll_ra_info *ra = &sbi->ll_ra_info;
1341 unsigned long throttle;
1342 unsigned long start = ras_align(ras, ras->ras_next_readahead, NULL);
1343 unsigned long end = start + pages - 1;
1345 throttle = min(ra->ra_async_pages_per_file_threshold,
1346 ra->ra_max_pages_per_file);
1348 * If this is strided i/o or the window is smaller than the
1349 * throttle limit, we do not do async readahead. Otherwise,
1350 * we do async readahead, allowing the user thread to do fast i/o.
1352 if (stride_io_mode(ras) || !throttle ||
1353 ras->ras_window_len < throttle)
1356 if ((atomic_read(&ra->ra_cur_pages) + pages) > ra->ra_max_pages)
1359 if (ras->ras_async_last_readpage == start)
1362 /* ll_readahead_work_free() free it */
1365 lrw->lrw_file = get_file(file);
1366 lrw->lrw_start = start;
1368 spin_lock(&ras->ras_lock);
1369 ras->ras_next_readahead = end + 1;
1370 ras->ras_async_last_readpage = start;
1371 spin_unlock(&ras->ras_lock);
1372 ll_readahead_work_add(inode, lrw);
1380 int ll_readpage(struct file *file, struct page *vmpage)
1382 struct inode *inode = file_inode(file);
1383 struct cl_object *clob = ll_i2info(inode)->lli_clob;
1384 struct ll_cl_context *lcc;
1385 const struct lu_env *env = NULL;
1386 struct cl_io *io = NULL;
1387 struct cl_page *page;
1388 struct ll_sb_info *sbi = ll_i2sbi(inode);
1392 lcc = ll_cl_find(file);
1398 if (io == NULL) { /* fast read */
1399 struct inode *inode = file_inode(file);
1400 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1401 struct ll_readahead_state *ras = &fd->fd_ras;
1402 struct lu_env *local_env = NULL;
1403 unsigned long fast_read_pages =
1404 max(RA_REMAIN_WINDOW_MIN, ras->ras_rpc_size);
1405 struct vvp_page *vpg;
1409 /* TODO: need to verify the layout version to make sure
1410 * the page is not invalid due to layout change. */
1411 page = cl_vmpage_page(vmpage, clob);
1413 unlock_page(vmpage);
1414 ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1418 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1419 if (vpg->vpg_defer_uptodate) {
1420 enum ras_update_flags flags = LL_RAS_HIT;
1422 if (lcc && lcc->lcc_type == LCC_MMAP)
1423 flags |= LL_RAS_MMAP;
1425 /* For fast read, it updates read ahead state only
1426 * if the page is hit in cache because non cache page
1427 * case will be handled by slow read later. */
1428 ras_update(sbi, inode, ras, vvp_index(vpg), flags);
1429 /* avoid duplicate ras_update() call */
1430 vpg->vpg_ra_updated = 1;
1432 /* Check if we can issue a readahead RPC, if that is
1433 * the case, we can't do fast IO because we will need
1434 * a cl_io to issue the RPC. */
1435 if (ras->ras_window_start + ras->ras_window_len <
1436 ras->ras_next_readahead + fast_read_pages ||
1437 kickoff_async_readahead(file, fast_read_pages) > 0)
1442 local_env = cl_env_percpu_get();
1446 /* export the page and skip io stack */
1448 vpg->vpg_ra_used = 1;
1449 cl_page_export(env, page, 1);
1451 ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1453 /* release page refcount before unlocking the page to ensure
1454 * the object won't be destroyed in the calling path of
1455 * cl_page_put(). Please see comment in ll_releasepage(). */
1456 cl_page_put(env, page);
1457 unlock_page(vmpage);
1459 cl_env_percpu_put(local_env);
1464 LASSERT(io->ci_state == CIS_IO_GOING);
1465 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
1466 if (!IS_ERR(page)) {
1467 LASSERT(page->cp_type == CPT_CACHEABLE);
1468 if (likely(!PageUptodate(vmpage))) {
1469 cl_page_assume(env, io, page);
1471 result = ll_io_read_page(env, io, page, file);
1473 /* Page from a non-object file. */
1474 unlock_page(vmpage);
1477 cl_page_put(env, page);
1479 unlock_page(vmpage);
1480 result = PTR_ERR(page);