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 "lre %lu cr %lu cb %lu ws %lu wl %lu nra %lu rpc %lu " \
134 "r %lu ri %lu csr %lu sf %lu sb %lu sl %lu lr %lu\n", \
135 ras->ras_last_read_end, ras->ras_consecutive_requests, \
136 ras->ras_consecutive_bytes, 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_bytes, ras->ras_stride_length, \
142 ras->ras_async_last_readpage)
144 static int pos_in_window(unsigned long pos, unsigned long point,
145 unsigned long before, unsigned long after)
147 unsigned long start = point - before, end = point + after;
154 return start <= pos && pos <= 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 rb %lu\n", \
246 ria->ria_start, ria->ria_end, ria->ria_stoff, \
247 ria->ria_length, ria->ria_bytes)
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_bytes = st_bytes
259 * |------------------|*****|------------------|*****|------------|*****|....
262 * |----- st_len -----|
264 * How many bytes it should read in such pattern
265 * |-------------------------------------------------------------|
267 * |<------ length ------->|
269 * = |<----->| + |-------------------------------------| + |---|
270 * start_left st_bytes * i end_left
273 stride_byte_count(unsigned long st_off, unsigned long st_len,
274 unsigned long st_bytes, unsigned long off,
275 unsigned long length)
277 __u64 start = off > st_off ? off - st_off : 0;
278 __u64 end = off + length > st_off ? off + length - st_off : 0;
279 unsigned long start_left = 0;
280 unsigned long end_left = 0;
281 unsigned long bytes_count;
283 if (st_len == 0 || length == 0 || end == 0)
286 start_left = do_div(start, st_len);
287 if (start_left < st_bytes)
288 start_left = st_bytes - start_left;
292 end_left = do_div(end, st_len);
293 if (end_left > st_bytes)
296 CDEBUG(D_READA, "start %llu, end %llu start_left %lu end_left %lu\n",
297 start, end, start_left, end_left);
300 bytes_count = end_left - (st_bytes - start_left);
302 bytes_count = start_left +
303 st_bytes * (end - start - 1) + end_left;
306 "st_off %lu, st_len %lu st_bytes %lu off %lu length %lu bytescount %lu\n",
307 st_off, st_len, st_bytes, off, length, bytes_count);
312 static int ria_page_count(struct ra_io_arg *ria)
314 __u64 length = ria->ria_end >= ria->ria_start ?
315 ria->ria_end - ria->ria_start + 1 : 0;
316 unsigned int bytes_count;
318 bytes_count = stride_byte_count(ria->ria_stoff, ria->ria_length,
319 ria->ria_bytes, ria->ria_start,
320 length << PAGE_SHIFT);
321 return (bytes_count + PAGE_SIZE - 1) >> PAGE_SHIFT;
325 static unsigned long ras_align(struct ll_readahead_state *ras,
326 pgoff_t index, unsigned long *remainder)
328 unsigned long rem = index % ras->ras_rpc_size;
329 if (remainder != NULL)
334 /*Check whether the index is in the defined ra-window */
335 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
337 /* If ria_length == ria_pages, it means non-stride I/O mode,
338 * idx should always inside read-ahead window in this case
339 * For stride I/O mode, just check whether the idx is inside
341 return ria->ria_length == 0 || ria->ria_length == ria->ria_bytes ||
342 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
343 ria->ria_length < ria->ria_bytes);
347 ll_read_ahead_pages(const struct lu_env *env, struct cl_io *io,
348 struct cl_page_list *queue, struct ll_readahead_state *ras,
349 struct ra_io_arg *ria, pgoff_t *ra_end)
351 struct cl_read_ahead ra = { 0 };
352 int rc = 0, count = 0;
356 LASSERT(ria != NULL);
359 stride_ria = ria->ria_length > ria->ria_bytes && ria->ria_bytes > 0;
360 for (page_idx = ria->ria_start;
361 page_idx <= ria->ria_end && ria->ria_reserved > 0; page_idx++) {
362 if (ras_inside_ra_window(page_idx, ria)) {
363 if (ra.cra_end == 0 || ra.cra_end < page_idx) {
366 cl_read_ahead_release(env, &ra);
368 rc = cl_io_read_ahead(env, io, page_idx, &ra);
372 /* Do not shrink the ria_end at any case until
373 * the minimum end of current read is covered.
374 * And only shrink the ria_end if the matched
375 * LDLM lock doesn't cover more. */
376 if (page_idx > ra.cra_end ||
377 (ra.cra_contention &&
378 page_idx > ria->ria_end_min)) {
379 ria->ria_end = ra.cra_end;
383 CDEBUG(D_READA, "idx: %lu, ra: %lu, rpc: %lu\n",
384 page_idx, ra.cra_end, ra.cra_rpc_size);
385 LASSERTF(ra.cra_end >= page_idx,
386 "object: %p, indcies %lu / %lu\n",
387 io->ci_obj, ra.cra_end, page_idx);
388 /* update read ahead RPC size.
389 * NB: it's racy but doesn't matter */
390 if (ras->ras_rpc_size != ra.cra_rpc_size &&
392 ras->ras_rpc_size = ra.cra_rpc_size;
393 /* trim it to align with optimal RPC size */
394 end = ras_align(ras, ria->ria_end + 1, NULL);
395 if (end > 0 && !ria->ria_eof)
396 ria->ria_end = end - 1;
397 if (ria->ria_end < ria->ria_end_min)
398 ria->ria_end = ria->ria_end_min;
400 if (page_idx > ria->ria_end)
403 /* If the page is inside the read-ahead window */
404 rc = ll_read_ahead_page(env, io, queue, page_idx);
409 /* Only subtract from reserve & count the page if we
410 * really did readahead on that page. */
415 } else if (stride_ria) {
416 /* If it is not in the read-ahead window, and it is
417 * read-ahead mode, then check whether it should skip
420 unsigned long offset;
421 unsigned long pos = page_idx << PAGE_SHIFT;
423 offset = (pos - ria->ria_stoff) % ria->ria_length;
424 if (offset >= ria->ria_bytes) {
425 pos += (ria->ria_length - offset);
426 page_idx = (pos >> PAGE_SHIFT) - 1;
428 "Stride: jump %lu pages to %lu\n",
429 ria->ria_length - offset, page_idx);
435 cl_read_ahead_release(env, &ra);
440 static void ll_readahead_work_free(struct ll_readahead_work *work)
442 fput(work->lrw_file);
446 static void ll_readahead_handle_work(struct work_struct *wq);
447 static void ll_readahead_work_add(struct inode *inode,
448 struct ll_readahead_work *work)
450 INIT_WORK(&work->lrw_readahead_work, ll_readahead_handle_work);
451 queue_work(ll_i2sbi(inode)->ll_ra_info.ll_readahead_wq,
452 &work->lrw_readahead_work);
455 static int ll_readahead_file_kms(const struct lu_env *env,
456 struct cl_io *io, __u64 *kms)
458 struct cl_object *clob;
460 struct cl_attr *attr = vvp_env_thread_attr(env);
464 inode = vvp_object_inode(clob);
466 cl_object_attr_lock(clob);
467 ret = cl_object_attr_get(env, clob, attr);
468 cl_object_attr_unlock(clob);
473 *kms = attr->cat_kms;
477 static void ll_readahead_handle_work(struct work_struct *wq)
479 struct ll_readahead_work *work;
482 struct ra_io_arg *ria;
484 struct ll_file_data *fd;
485 struct ll_readahead_state *ras;
487 struct cl_2queue *queue;
489 unsigned long len, mlen = 0;
493 unsigned long end_index;
495 work = container_of(wq, struct ll_readahead_work,
497 fd = LUSTRE_FPRIVATE(work->lrw_file);
499 file = work->lrw_file;
500 inode = file_inode(file);
502 env = cl_env_alloc(&refcheck, LCT_NOREF);
504 GOTO(out_free_work, rc = PTR_ERR(env));
506 io = vvp_env_thread_io(env);
507 ll_io_init(io, file, CIT_READ, NULL);
509 rc = ll_readahead_file_kms(env, io, &kms);
511 GOTO(out_put_env, rc);
514 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
515 GOTO(out_put_env, rc = 0);
518 ria = &ll_env_info(env)->lti_ria;
519 memset(ria, 0, sizeof(*ria));
521 ria->ria_start = work->lrw_start;
522 /* Truncate RA window to end of file */
523 end_index = (unsigned long)((kms - 1) >> PAGE_SHIFT);
524 if (end_index <= work->lrw_end) {
525 work->lrw_end = end_index;
528 if (work->lrw_end <= work->lrw_start)
529 GOTO(out_put_env, rc = 0);
531 ria->ria_end = work->lrw_end;
532 len = ria->ria_end - ria->ria_start + 1;
533 ria->ria_reserved = ll_ra_count_get(ll_i2sbi(inode), ria,
534 ria_page_count(ria), mlen);
537 "async reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
538 ria->ria_reserved, len, mlen,
539 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
540 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
542 if (ria->ria_reserved < len) {
543 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
544 if (PAGES_TO_MiB(ria->ria_reserved) < 1) {
545 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
546 GOTO(out_put_env, rc = 0);
550 rc = cl_io_rw_init(env, io, CIT_READ, ria->ria_start, len);
552 GOTO(out_put_env, rc);
554 vvp_env_io(env)->vui_io_subtype = IO_NORMAL;
555 vvp_env_io(env)->vui_fd = fd;
556 io->ci_state = CIS_LOCKED;
557 io->ci_async_readahead = true;
558 rc = cl_io_start(env, io);
560 GOTO(out_io_fini, rc);
562 queue = &io->ci_queue;
563 cl_2queue_init(queue);
565 rc = ll_read_ahead_pages(env, io, &queue->c2_qin, ras, ria, &ra_end);
566 if (ria->ria_reserved != 0)
567 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
568 if (queue->c2_qin.pl_nr > 0) {
569 int count = queue->c2_qin.pl_nr;
571 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
573 task_io_account_read(PAGE_SIZE * count);
575 if (ria->ria_end == ra_end && ra_end == (kms >> PAGE_SHIFT))
576 ll_ra_stats_inc(inode, RA_STAT_EOF);
578 if (ra_end != ria->ria_end)
579 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
581 /* TODO: discard all pages until page reinit route is implemented */
582 cl_page_list_discard(env, io, &queue->c2_qin);
584 /* Unlock unsent read pages in case of error. */
585 cl_page_list_disown(env, io, &queue->c2_qin);
587 cl_2queue_fini(env, queue);
592 cl_env_put(env, &refcheck);
595 ll_ra_stats_inc_sbi(ll_i2sbi(inode), RA_STAT_ASYNC);
596 ll_readahead_work_free(work);
599 static int ll_readahead(const struct lu_env *env, struct cl_io *io,
600 struct cl_page_list *queue,
601 struct ll_readahead_state *ras, bool hit,
604 struct vvp_io *vio = vvp_env_io(env);
605 struct ll_thread_info *lti = ll_env_info(env);
606 unsigned long len, mlen = 0;
607 pgoff_t ra_end = 0, start = 0, end = 0;
609 struct ra_io_arg *ria = <i->lti_ria;
610 struct cl_object *clob;
616 inode = vvp_object_inode(clob);
618 memset(ria, 0, sizeof *ria);
619 ret = ll_readahead_file_kms(env, io, &kms);
624 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
628 spin_lock(&ras->ras_lock);
631 * Note: other thread might rollback the ras_next_readahead,
632 * if it can not get the full size of prepared pages, see the
633 * end of this function. For stride read ahead, it needs to
634 * make sure the offset is no less than ras_stride_offset,
635 * so that stride read ahead can work correctly.
637 if (stride_io_mode(ras))
638 start = max(ras->ras_next_readahead,
639 ras->ras_stride_offset >> PAGE_SHIFT);
641 start = ras->ras_next_readahead;
643 if (ras->ras_window_len > 0)
644 end = ras->ras_window_start + ras->ras_window_len - 1;
646 /* Enlarge the RA window to encompass the full read */
647 if (vio->vui_ra_valid &&
648 end < vio->vui_ra_start + vio->vui_ra_count - 1)
649 end = vio->vui_ra_start + vio->vui_ra_count - 1;
652 unsigned long end_index;
654 /* Truncate RA window to end of file */
655 end_index = (unsigned long)((kms - 1) >> PAGE_SHIFT);
656 if (end_index <= end) {
661 ria->ria_start = start;
663 /* If stride I/O mode is detected, get stride window*/
664 if (stride_io_mode(ras)) {
665 ria->ria_stoff = ras->ras_stride_offset;
666 ria->ria_length = ras->ras_stride_length;
667 ria->ria_bytes = ras->ras_stride_bytes;
669 spin_unlock(&ras->ras_lock);
672 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
675 len = ria_page_count(ria);
677 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
682 CDEBUG(D_READA, DFID": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
683 PFID(lu_object_fid(&clob->co_lu)),
684 ria->ria_start, ria->ria_end,
685 vio->vui_ra_valid ? vio->vui_ra_start : 0,
686 vio->vui_ra_valid ? vio->vui_ra_count : 0,
689 /* at least to extend the readahead window to cover current read */
690 if (!hit && vio->vui_ra_valid &&
691 vio->vui_ra_start + vio->vui_ra_count > ria->ria_start)
692 ria->ria_end_min = vio->vui_ra_start + vio->vui_ra_count - 1;
694 ria->ria_reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len, mlen);
695 if (ria->ria_reserved < len)
696 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
698 CDEBUG(D_READA, "reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
699 ria->ria_reserved, len, mlen,
700 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
701 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
703 ret = ll_read_ahead_pages(env, io, queue, ras, ria, &ra_end);
705 if (ria->ria_reserved != 0)
706 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
708 if (ra_end == end && ra_end == (kms >> PAGE_SHIFT))
709 ll_ra_stats_inc(inode, RA_STAT_EOF);
711 CDEBUG(D_READA, "ra_end = %lu end = %lu stride end = %lu pages = %d\n",
712 ra_end, end, ria->ria_end, ret);
715 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
717 /* update the ras so that the next read-ahead tries from
718 * where we left off. */
719 spin_lock(&ras->ras_lock);
720 ras->ras_next_readahead = ra_end + 1;
721 spin_unlock(&ras->ras_lock);
728 static void ras_set_start(struct ll_readahead_state *ras, pgoff_t index)
730 ras->ras_window_start = ras_align(ras, index, NULL);
733 /* called with the ras_lock held or from places where it doesn't matter */
734 static void ras_reset(struct ll_readahead_state *ras, pgoff_t index)
736 ras->ras_consecutive_requests = 0;
737 ras->ras_consecutive_bytes = 0;
738 ras->ras_window_len = 0;
739 ras_set_start(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_bytes = 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;
759 ras->ras_last_read_end = 0;
760 ras->ras_requests = 0;
764 * Check whether the read request is in the stride window.
765 * If it is in the stride window, return true, otherwise return false.
767 static bool index_in_stride_window(struct ll_readahead_state *ras,
770 unsigned long stride_gap;
771 unsigned long pos = index << PAGE_SHIFT;
773 if (ras->ras_stride_length == 0 || ras->ras_stride_bytes == 0 ||
774 ras->ras_stride_bytes == ras->ras_stride_length)
777 stride_gap = pos - ras->ras_last_read_end - 1;
779 /* If it is contiguous read */
781 return ras->ras_consecutive_bytes + PAGE_SIZE <=
782 ras->ras_stride_bytes;
784 /* Otherwise check the stride by itself */
785 return (ras->ras_stride_length - ras->ras_stride_bytes) == stride_gap &&
786 ras->ras_consecutive_bytes == ras->ras_stride_bytes;
789 static void ras_init_stride_detector(struct ll_readahead_state *ras,
790 unsigned long pos, unsigned long count)
792 unsigned long stride_gap = pos - ras->ras_last_read_end - 1;
794 if (!stride_io_mode(ras) && (stride_gap != 0 ||
795 ras->ras_consecutive_stride_requests == 0)) {
796 ras->ras_stride_bytes = ras->ras_consecutive_bytes;
797 ras->ras_stride_length = stride_gap + ras->ras_consecutive_bytes;
799 LASSERT(ras->ras_request_index == 0);
800 LASSERT(ras->ras_consecutive_stride_requests == 0);
802 if (pos <= ras->ras_last_read_end) {
803 /*Reset stride window for forward read*/
804 ras_stride_reset(ras);
808 ras->ras_stride_bytes = ras->ras_consecutive_bytes;
809 ras->ras_stride_length = stride_gap + ras->ras_consecutive_bytes;
815 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
817 unsigned int bytes_count =
818 stride_byte_count(ras->ras_stride_offset,
819 ras->ras_stride_length, ras->ras_stride_bytes,
820 ras->ras_stride_offset, len);
822 return (bytes_count + PAGE_SIZE - 1) >> PAGE_SHIFT;
825 /* Stride Read-ahead window will be increased inc_len according to
826 * stride I/O pattern */
827 static void ras_stride_increase_window(struct ll_readahead_state *ras,
828 struct ll_ra_info *ra,
829 unsigned long inc_len)
831 unsigned long left, step, window_len;
832 unsigned long stride_len;
833 unsigned long end = ras->ras_window_start + ras->ras_window_len;
835 LASSERT(ras->ras_stride_length > 0);
836 LASSERTF(end >= (ras->ras_stride_offset >> PAGE_SHIFT),
837 "window_start %lu, window_len %lu stride_offset %lu\n",
838 ras->ras_window_start, ras->ras_window_len,
839 ras->ras_stride_offset);
842 if (end < ras->ras_stride_offset)
845 stride_len = end - ras->ras_stride_offset;
847 left = stride_len % ras->ras_stride_length;
848 window_len = (ras->ras_window_len << PAGE_SHIFT) - left;
850 if (left < ras->ras_stride_bytes)
853 left = ras->ras_stride_bytes + inc_len;
855 LASSERT(ras->ras_stride_bytes != 0);
857 step = left / ras->ras_stride_bytes;
858 left %= ras->ras_stride_bytes;
860 window_len += step * ras->ras_stride_length + left;
862 if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
863 ras->ras_window_len = (window_len >> PAGE_SHIFT);
868 static void ras_increase_window(struct inode *inode,
869 struct ll_readahead_state *ras,
870 struct ll_ra_info *ra)
872 /* The stretch of ra-window should be aligned with max rpc_size
873 * but current clio architecture does not support retrieve such
874 * information from lower layer. FIXME later
876 if (stride_io_mode(ras)) {
877 ras_stride_increase_window(ras, ra,
878 ras->ras_rpc_size << PAGE_SHIFT);
882 wlen = min(ras->ras_window_len + ras->ras_rpc_size,
883 ra->ra_max_pages_per_file);
884 if (wlen < ras->ras_rpc_size)
885 ras->ras_window_len = wlen;
887 ras->ras_window_len = ras_align(ras, wlen, NULL);
891 static void ras_update(struct ll_sb_info *sbi, struct inode *inode,
892 struct ll_readahead_state *ras, unsigned long index,
893 enum ras_update_flags flags)
895 struct ll_ra_info *ra = &sbi->ll_ra_info;
896 bool hit = flags & LL_RAS_HIT;
897 int zero = 0, stride_detect = 0, ra_miss = 0;
898 unsigned long pos = index << PAGE_SHIFT;
901 spin_lock(&ras->ras_lock);
904 CDEBUG(D_READA, DFID " pages at %lu miss.\n",
905 PFID(ll_inode2fid(inode)), index);
906 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
908 /* reset the read-ahead window in two cases. First when the app seeks
909 * or reads to some other part of the file. Secondly if we get a
910 * read-ahead miss that we think we've previously issued. This can
911 * be a symptom of there being so many read-ahead pages that the VM is
912 * reclaiming it before we get to it. */
913 if (!pos_in_window(pos, ras->ras_last_read_end,
914 8 << PAGE_SHIFT, 8 << PAGE_SHIFT)) {
916 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
917 } else if (!hit && ras->ras_window_len &&
918 index < ras->ras_next_readahead &&
919 pos_in_window(index, ras->ras_window_start, 0,
920 ras->ras_window_len)) {
922 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
925 /* On the second access to a file smaller than the tunable
926 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
927 * file up to ra_max_pages_per_file. This is simply a best effort
928 * and only occurs once per open file. Normal RA behavior is reverted
929 * to for subsequent IO. The mmap case does not increment
930 * ras_requests and thus can never trigger this behavior. */
931 if (ras->ras_requests >= 2 && !ras->ras_request_index) {
934 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
937 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
938 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
941 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
942 ras->ras_window_start = 0;
943 ras->ras_next_readahead = index + 1;
944 ras->ras_window_len = min(ra->ra_max_pages_per_file,
945 ra->ra_max_read_ahead_whole_pages);
950 /* check whether it is in stride I/O mode*/
951 if (!index_in_stride_window(ras, index)) {
952 if (ras->ras_consecutive_stride_requests == 0 &&
953 ras->ras_request_index == 0) {
954 ras_init_stride_detector(ras, pos, PAGE_SIZE);
955 ras->ras_consecutive_stride_requests++;
957 ras_stride_reset(ras);
959 ras_reset(ras, index);
960 ras->ras_consecutive_bytes += PAGE_SIZE;
963 ras->ras_consecutive_bytes = 0;
964 ras->ras_consecutive_requests = 0;
965 if (++ras->ras_consecutive_stride_requests > 1)
971 if (index_in_stride_window(ras, index) &&
972 stride_io_mode(ras)) {
973 if (index != (ras->ras_last_read_end >>
975 ras->ras_consecutive_bytes = 0;
976 ras_reset(ras, index);
978 /* If stride-RA hit cache miss, the stride
979 * detector will not be reset to avoid the
980 * overhead of redetecting read-ahead mode,
981 * but on the condition that the stride window
982 * is still intersect with normal sequential
983 * read-ahead window. */
984 if (ras->ras_window_start <
985 (ras->ras_stride_offset >> PAGE_SHIFT))
986 ras_stride_reset(ras);
989 /* Reset both stride window and normal RA
991 ras_reset(ras, index);
992 ras->ras_consecutive_bytes += PAGE_SIZE;
993 ras_stride_reset(ras);
996 } else if (stride_io_mode(ras)) {
997 /* If this is contiguous read but in stride I/O mode
998 * currently, check whether stride step still is valid,
999 * if invalid, it will reset the stride ra window*/
1000 if (!index_in_stride_window(ras, index)) {
1001 /* Shrink stride read-ahead window to be zero */
1002 ras_stride_reset(ras);
1003 ras->ras_window_len = 0;
1004 ras->ras_next_readahead = index;
1008 ras->ras_consecutive_bytes += PAGE_SIZE;
1009 ras_set_start(ras, index);
1011 if (stride_io_mode(ras)) {
1012 /* Since stride readahead is sentivite to the offset
1013 * of read-ahead, so we use original offset here,
1014 * instead of ras_window_start, which is RPC aligned */
1015 ras->ras_next_readahead = max(index + 1,
1016 ras->ras_next_readahead);
1017 ras->ras_window_start =
1018 max(ras->ras_stride_offset >> PAGE_SHIFT,
1019 ras->ras_window_start);
1021 if (ras->ras_next_readahead < ras->ras_window_start)
1022 ras->ras_next_readahead = ras->ras_window_start;
1024 ras->ras_next_readahead = index + 1;
1028 /* Trigger RA in the mmap case where ras_consecutive_requests
1029 * is not incremented and thus can't be used to trigger RA */
1030 if (ras->ras_consecutive_bytes >= (4 << PAGE_SHIFT) &&
1031 flags & LL_RAS_MMAP) {
1032 ras_increase_window(inode, ras, ra);
1033 /* reset consecutive pages so that the readahead window can
1034 * grow gradually. */
1035 ras->ras_consecutive_bytes = 0;
1036 GOTO(out_unlock, 0);
1039 /* Initially reset the stride window offset to next_readahead*/
1040 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
1042 * Once stride IO mode is detected, next_readahead should be
1043 * reset to make sure next_readahead > stride offset
1045 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
1046 ras->ras_stride_offset = index << PAGE_SHIFT;
1047 ras->ras_window_start = max(index, ras->ras_window_start);
1050 /* The initial ras_window_len is set to the request size. To avoid
1051 * uselessly reading and discarding pages for random IO the window is
1052 * only increased once per consecutive request received. */
1053 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
1054 !ras->ras_request_index)
1055 ras_increase_window(inode, ras, ra);
1059 ras->ras_request_index++;
1060 ras->ras_last_read_end = pos + PAGE_SIZE - 1;
1061 spin_unlock(&ras->ras_lock);
1064 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1066 struct inode *inode = vmpage->mapping->host;
1067 struct ll_inode_info *lli = ll_i2info(inode);
1070 struct cl_page *page;
1071 struct cl_object *clob;
1072 bool redirtied = false;
1073 bool unlocked = false;
1078 LASSERT(PageLocked(vmpage));
1079 LASSERT(!PageWriteback(vmpage));
1081 LASSERT(ll_i2dtexp(inode) != NULL);
1083 env = cl_env_get(&refcheck);
1085 GOTO(out, result = PTR_ERR(env));
1087 clob = ll_i2info(inode)->lli_clob;
1088 LASSERT(clob != NULL);
1090 io = vvp_env_thread_io(env);
1092 io->ci_ignore_layout = 1;
1093 result = cl_io_init(env, io, CIT_MISC, clob);
1095 page = cl_page_find(env, clob, vmpage->index,
1096 vmpage, CPT_CACHEABLE);
1097 if (!IS_ERR(page)) {
1098 lu_ref_add(&page->cp_reference, "writepage",
1100 cl_page_assume(env, io, page);
1101 result = cl_page_flush(env, io, page);
1104 * Re-dirty page on error so it retries write,
1105 * but not in case when IO has actually
1106 * occurred and completed with an error.
1108 if (!PageError(vmpage)) {
1109 redirty_page_for_writepage(wbc, vmpage);
1114 cl_page_disown(env, io, page);
1116 lu_ref_del(&page->cp_reference,
1117 "writepage", current);
1118 cl_page_put(env, page);
1120 result = PTR_ERR(page);
1123 cl_io_fini(env, io);
1125 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1126 loff_t offset = cl_offset(clob, vmpage->index);
1128 /* Flush page failed because the extent is being written out.
1129 * Wait for the write of extent to be finished to avoid
1130 * breaking kernel which assumes ->writepage should mark
1131 * PageWriteback or clean the page. */
1132 result = cl_sync_file_range(inode, offset,
1133 offset + PAGE_SIZE - 1,
1136 /* actually we may have written more than one page.
1137 * decreasing this page because the caller will count
1139 wbc->nr_to_write -= result - 1;
1144 cl_env_put(env, &refcheck);
1149 if (!lli->lli_async_rc)
1150 lli->lli_async_rc = result;
1151 SetPageError(vmpage);
1153 unlock_page(vmpage);
1158 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1160 struct inode *inode = mapping->host;
1163 enum cl_fsync_mode mode;
1164 int range_whole = 0;
1168 if (wbc->range_cyclic) {
1169 start = mapping->writeback_index << PAGE_SHIFT;
1170 end = OBD_OBJECT_EOF;
1172 start = wbc->range_start;
1173 end = wbc->range_end;
1174 if (end == LLONG_MAX) {
1175 end = OBD_OBJECT_EOF;
1176 range_whole = start == 0;
1180 mode = CL_FSYNC_NONE;
1181 if (wbc->sync_mode == WB_SYNC_ALL)
1182 mode = CL_FSYNC_LOCAL;
1184 if (ll_i2info(inode)->lli_clob == NULL)
1187 /* for directio, it would call writepages() to evict cached pages
1188 * inside the IO context of write, which will cause deadlock at
1189 * layout_conf since it waits for active IOs to complete. */
1190 result = cl_sync_file_range(inode, start, end, mode, 1);
1192 wbc->nr_to_write -= result;
1196 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1197 if (end == OBD_OBJECT_EOF)
1198 mapping->writeback_index = 0;
1200 mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
1205 struct ll_cl_context *ll_cl_find(struct file *file)
1207 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1208 struct ll_cl_context *lcc;
1209 struct ll_cl_context *found = NULL;
1211 read_lock(&fd->fd_lock);
1212 list_for_each_entry(lcc, &fd->fd_lccs, lcc_list) {
1213 if (lcc->lcc_cookie == current) {
1218 read_unlock(&fd->fd_lock);
1223 void ll_cl_add(struct file *file, const struct lu_env *env, struct cl_io *io,
1226 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1227 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1229 memset(lcc, 0, sizeof(*lcc));
1230 INIT_LIST_HEAD(&lcc->lcc_list);
1231 lcc->lcc_cookie = current;
1234 lcc->lcc_type = type;
1236 write_lock(&fd->fd_lock);
1237 list_add(&lcc->lcc_list, &fd->fd_lccs);
1238 write_unlock(&fd->fd_lock);
1241 void ll_cl_remove(struct file *file, const struct lu_env *env)
1243 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1244 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1246 write_lock(&fd->fd_lock);
1247 list_del_init(&lcc->lcc_list);
1248 write_unlock(&fd->fd_lock);
1251 int ll_io_read_page(const struct lu_env *env, struct cl_io *io,
1252 struct cl_page *page, struct file *file)
1254 struct inode *inode = vvp_object_inode(page->cp_obj);
1255 struct ll_sb_info *sbi = ll_i2sbi(inode);
1256 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1257 struct ll_readahead_state *ras = &fd->fd_ras;
1258 struct cl_2queue *queue = &io->ci_queue;
1259 struct cl_sync_io *anchor = NULL;
1260 struct vvp_page *vpg;
1265 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1266 uptodate = vpg->vpg_defer_uptodate;
1268 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1269 sbi->ll_ra_info.ra_max_pages > 0 &&
1270 !vpg->vpg_ra_updated) {
1271 struct vvp_io *vio = vvp_env_io(env);
1272 enum ras_update_flags flags = 0;
1275 flags |= LL_RAS_HIT;
1276 if (!vio->vui_ra_valid)
1277 flags |= LL_RAS_MMAP;
1278 ras_update(sbi, inode, ras, vvp_index(vpg), flags);
1281 cl_2queue_init(queue);
1283 vpg->vpg_ra_used = 1;
1284 cl_page_export(env, page, 1);
1285 cl_page_disown(env, io, page);
1287 anchor = &vvp_env_info(env)->vti_anchor;
1288 cl_sync_io_init(anchor, 1);
1289 page->cp_sync_io = anchor;
1291 cl_2queue_add(queue, page);
1294 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1295 sbi->ll_ra_info.ra_max_pages > 0) {
1298 rc2 = ll_readahead(env, io, &queue->c2_qin, ras,
1300 CDEBUG(D_READA, DFID "%d pages read ahead at %lu\n",
1301 PFID(ll_inode2fid(inode)), rc2, vvp_index(vpg));
1304 if (queue->c2_qin.pl_nr > 0) {
1305 int count = queue->c2_qin.pl_nr;
1306 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
1308 task_io_account_read(PAGE_SIZE * count);
1312 if (anchor != NULL && !cl_page_is_owned(page, io)) { /* have sent */
1313 rc = cl_sync_io_wait(env, anchor, 0);
1315 cl_page_assume(env, io, page);
1316 cl_page_list_del(env, &queue->c2_qout, page);
1318 if (!PageUptodate(cl_page_vmpage(page))) {
1319 /* Failed to read a mirror, discard this page so that
1320 * new page can be created with new mirror.
1322 * TODO: this is not needed after page reinit
1323 * route is implemented */
1324 cl_page_discard(env, io, page);
1326 cl_page_disown(env, io, page);
1329 /* TODO: discard all pages until page reinit route is implemented */
1330 cl_page_list_discard(env, io, &queue->c2_qin);
1332 /* Unlock unsent read pages in case of error. */
1333 cl_page_list_disown(env, io, &queue->c2_qin);
1335 cl_2queue_fini(env, queue);
1341 * Possible return value:
1342 * 0 no async readahead triggered and fast read could not be used.
1343 * 1 no async readahead, but fast read could be used.
1344 * 2 async readahead triggered and fast read could be used too.
1347 static int kickoff_async_readahead(struct file *file, unsigned long pages)
1349 struct ll_readahead_work *lrw;
1350 struct inode *inode = file_inode(file);
1351 struct ll_sb_info *sbi = ll_i2sbi(inode);
1352 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1353 struct ll_readahead_state *ras = &fd->fd_ras;
1354 struct ll_ra_info *ra = &sbi->ll_ra_info;
1355 unsigned long throttle;
1356 unsigned long start = ras_align(ras, ras->ras_next_readahead, NULL);
1357 unsigned long end = start + pages - 1;
1359 throttle = min(ra->ra_async_pages_per_file_threshold,
1360 ra->ra_max_pages_per_file);
1362 * If this is strided i/o or the window is smaller than the
1363 * throttle limit, we do not do async readahead. Otherwise,
1364 * we do async readahead, allowing the user thread to do fast i/o.
1366 if (stride_io_mode(ras) || !throttle ||
1367 ras->ras_window_len < throttle)
1370 if ((atomic_read(&ra->ra_cur_pages) + pages) > ra->ra_max_pages)
1373 if (ras->ras_async_last_readpage == start)
1376 /* ll_readahead_work_free() free it */
1379 lrw->lrw_file = get_file(file);
1380 lrw->lrw_start = start;
1382 spin_lock(&ras->ras_lock);
1383 ras->ras_next_readahead = end + 1;
1384 ras->ras_async_last_readpage = start;
1385 spin_unlock(&ras->ras_lock);
1386 ll_readahead_work_add(inode, lrw);
1394 int ll_readpage(struct file *file, struct page *vmpage)
1396 struct inode *inode = file_inode(file);
1397 struct cl_object *clob = ll_i2info(inode)->lli_clob;
1398 struct ll_cl_context *lcc;
1399 const struct lu_env *env = NULL;
1400 struct cl_io *io = NULL;
1401 struct cl_page *page;
1402 struct ll_sb_info *sbi = ll_i2sbi(inode);
1406 lcc = ll_cl_find(file);
1412 if (io == NULL) { /* fast read */
1413 struct inode *inode = file_inode(file);
1414 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1415 struct ll_readahead_state *ras = &fd->fd_ras;
1416 struct lu_env *local_env = NULL;
1417 unsigned long fast_read_pages =
1418 max(RA_REMAIN_WINDOW_MIN, ras->ras_rpc_size);
1419 struct vvp_page *vpg;
1423 /* TODO: need to verify the layout version to make sure
1424 * the page is not invalid due to layout change. */
1425 page = cl_vmpage_page(vmpage, clob);
1427 unlock_page(vmpage);
1428 ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1432 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1433 if (vpg->vpg_defer_uptodate) {
1434 enum ras_update_flags flags = LL_RAS_HIT;
1436 if (lcc && lcc->lcc_type == LCC_MMAP)
1437 flags |= LL_RAS_MMAP;
1439 /* For fast read, it updates read ahead state only
1440 * if the page is hit in cache because non cache page
1441 * case will be handled by slow read later. */
1442 ras_update(sbi, inode, ras, vvp_index(vpg), flags);
1443 /* avoid duplicate ras_update() call */
1444 vpg->vpg_ra_updated = 1;
1446 /* Check if we can issue a readahead RPC, if that is
1447 * the case, we can't do fast IO because we will need
1448 * a cl_io to issue the RPC. */
1449 if (ras->ras_window_start + ras->ras_window_len <
1450 ras->ras_next_readahead + fast_read_pages ||
1451 kickoff_async_readahead(file, fast_read_pages) > 0)
1456 local_env = cl_env_percpu_get();
1460 /* export the page and skip io stack */
1462 vpg->vpg_ra_used = 1;
1463 cl_page_export(env, page, 1);
1465 ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1467 /* release page refcount before unlocking the page to ensure
1468 * the object won't be destroyed in the calling path of
1469 * cl_page_put(). Please see comment in ll_releasepage(). */
1470 cl_page_put(env, page);
1471 unlock_page(vmpage);
1473 cl_env_percpu_put(local_env);
1478 LASSERT(io->ci_state == CIS_IO_GOING);
1479 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
1480 if (!IS_ERR(page)) {
1481 LASSERT(page->cp_type == CPT_CACHEABLE);
1482 if (likely(!PageUptodate(vmpage))) {
1483 cl_page_assume(env, io, page);
1485 result = ll_io_read_page(env, io, page, file);
1487 /* Page from a non-object file. */
1488 unlock_page(vmpage);
1491 cl_page_put(env, page);
1493 unlock_page(vmpage);
1494 result = PTR_ERR(page);