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;
816 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
818 unsigned int bytes_count =
819 stride_byte_count(ras->ras_stride_offset,
820 ras->ras_stride_length, ras->ras_stride_bytes,
821 ras->ras_stride_offset, len);
823 return (bytes_count + PAGE_SIZE - 1) >> PAGE_SHIFT;
826 /* Stride Read-ahead window will be increased inc_len according to
827 * stride I/O pattern */
828 static void ras_stride_increase_window(struct ll_readahead_state *ras,
829 struct ll_ra_info *ra,
830 unsigned long inc_len)
832 unsigned long left, step, window_len;
833 unsigned long stride_len;
834 unsigned long end = ras->ras_window_start + ras->ras_window_len;
836 LASSERT(ras->ras_stride_length > 0);
837 LASSERTF(end >= (ras->ras_stride_offset >> PAGE_SHIFT),
838 "window_start %lu, window_len %lu stride_offset %lu\n",
839 ras->ras_window_start, ras->ras_window_len,
840 ras->ras_stride_offset);
843 if (end < ras->ras_stride_offset)
846 stride_len = end - ras->ras_stride_offset;
848 left = stride_len % ras->ras_stride_length;
849 window_len = (ras->ras_window_len << PAGE_SHIFT) - left;
851 if (left < ras->ras_stride_bytes)
854 left = ras->ras_stride_bytes + inc_len;
856 LASSERT(ras->ras_stride_bytes != 0);
858 step = left / ras->ras_stride_bytes;
859 left %= ras->ras_stride_bytes;
861 window_len += step * ras->ras_stride_length + left;
863 if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
864 ras->ras_window_len = (window_len >> PAGE_SHIFT);
869 static void ras_increase_window(struct inode *inode,
870 struct ll_readahead_state *ras,
871 struct ll_ra_info *ra)
873 /* The stretch of ra-window should be aligned with max rpc_size
874 * but current clio architecture does not support retrieve such
875 * information from lower layer. FIXME later
877 if (stride_io_mode(ras)) {
878 ras_stride_increase_window(ras, ra,
879 ras->ras_rpc_size << PAGE_SHIFT);
883 wlen = min(ras->ras_window_len + ras->ras_rpc_size,
884 ra->ra_max_pages_per_file);
885 if (wlen < ras->ras_rpc_size)
886 ras->ras_window_len = wlen;
888 ras->ras_window_len = ras_align(ras, wlen, NULL);
892 static void ras_update(struct ll_sb_info *sbi, struct inode *inode,
893 struct ll_readahead_state *ras, unsigned long index,
894 enum ras_update_flags flags)
896 struct ll_ra_info *ra = &sbi->ll_ra_info;
897 bool hit = flags & LL_RAS_HIT;
898 int zero = 0, stride_detect = 0, ra_miss = 0;
899 unsigned long pos = index << PAGE_SHIFT;
902 spin_lock(&ras->ras_lock);
905 CDEBUG(D_READA, DFID " pages at %lu miss.\n",
906 PFID(ll_inode2fid(inode)), index);
907 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
909 /* reset the read-ahead window in two cases. First when the app seeks
910 * or reads to some other part of the file. Secondly if we get a
911 * read-ahead miss that we think we've previously issued. This can
912 * be a symptom of there being so many read-ahead pages that the VM is
913 * reclaiming it before we get to it. */
914 if (!pos_in_window(pos, ras->ras_last_read_end,
915 8 << PAGE_SHIFT, 8 << PAGE_SHIFT)) {
917 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
918 } else if (!hit && ras->ras_window_len &&
919 index < ras->ras_next_readahead &&
920 pos_in_window(index, ras->ras_window_start, 0,
921 ras->ras_window_len)) {
923 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
926 /* On the second access to a file smaller than the tunable
927 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
928 * file up to ra_max_pages_per_file. This is simply a best effort
929 * and only occurs once per open file. Normal RA behavior is reverted
930 * to for subsequent IO. The mmap case does not increment
931 * ras_requests and thus can never trigger this behavior. */
932 if (ras->ras_requests >= 2 && !ras->ras_request_index) {
935 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
938 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
939 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
942 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
943 ras->ras_window_start = 0;
944 ras->ras_next_readahead = index + 1;
945 ras->ras_window_len = min(ra->ra_max_pages_per_file,
946 ra->ra_max_read_ahead_whole_pages);
951 /* check whether it is in stride I/O mode*/
952 if (!index_in_stride_window(ras, index)) {
953 if (ras->ras_consecutive_stride_requests == 0 &&
954 ras->ras_request_index == 0) {
955 ras_init_stride_detector(ras, pos, PAGE_SIZE);
956 ras->ras_consecutive_stride_requests++;
958 ras_stride_reset(ras);
960 ras_reset(ras, index);
961 ras->ras_consecutive_bytes += PAGE_SIZE;
964 ras->ras_consecutive_bytes = 0;
965 ras->ras_consecutive_requests = 0;
966 if (++ras->ras_consecutive_stride_requests > 1)
972 if (index_in_stride_window(ras, index) &&
973 stride_io_mode(ras)) {
974 if (index != (ras->ras_last_read_end >>
976 ras->ras_consecutive_bytes = 0;
977 ras_reset(ras, index);
979 /* If stride-RA hit cache miss, the stride
980 * detector will not be reset to avoid the
981 * overhead of redetecting read-ahead mode,
982 * but on the condition that the stride window
983 * is still intersect with normal sequential
984 * read-ahead window. */
985 if (ras->ras_window_start <
986 (ras->ras_stride_offset >> PAGE_SHIFT))
987 ras_stride_reset(ras);
990 /* Reset both stride window and normal RA
992 ras_reset(ras, index);
993 ras->ras_consecutive_bytes += PAGE_SIZE;
994 ras_stride_reset(ras);
997 } else if (stride_io_mode(ras)) {
998 /* If this is contiguous read but in stride I/O mode
999 * currently, check whether stride step still is valid,
1000 * if invalid, it will reset the stride ra window*/
1001 if (!index_in_stride_window(ras, index)) {
1002 /* Shrink stride read-ahead window to be zero */
1003 ras_stride_reset(ras);
1004 ras->ras_window_len = 0;
1005 ras->ras_next_readahead = index;
1009 ras->ras_consecutive_bytes += PAGE_SIZE;
1010 ras_set_start(ras, index);
1012 if (stride_io_mode(ras)) {
1013 /* Since stride readahead is sentivite to the offset
1014 * of read-ahead, so we use original offset here,
1015 * instead of ras_window_start, which is RPC aligned */
1016 ras->ras_next_readahead = max(index + 1,
1017 ras->ras_next_readahead);
1018 ras->ras_window_start =
1019 max(ras->ras_stride_offset >> PAGE_SHIFT,
1020 ras->ras_window_start);
1022 if (ras->ras_next_readahead < ras->ras_window_start)
1023 ras->ras_next_readahead = ras->ras_window_start;
1025 ras->ras_next_readahead = index + 1;
1029 /* Trigger RA in the mmap case where ras_consecutive_requests
1030 * is not incremented and thus can't be used to trigger RA */
1031 if (ras->ras_consecutive_bytes >= (4 << PAGE_SHIFT) &&
1032 flags & LL_RAS_MMAP) {
1033 ras_increase_window(inode, ras, ra);
1034 /* reset consecutive pages so that the readahead window can
1035 * grow gradually. */
1036 ras->ras_consecutive_bytes = 0;
1037 GOTO(out_unlock, 0);
1040 /* Initially reset the stride window offset to next_readahead*/
1041 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
1043 * Once stride IO mode is detected, next_readahead should be
1044 * reset to make sure next_readahead > stride offset
1046 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
1047 ras->ras_stride_offset = index << PAGE_SHIFT;
1048 ras->ras_window_start = max(index, ras->ras_window_start);
1051 /* The initial ras_window_len is set to the request size. To avoid
1052 * uselessly reading and discarding pages for random IO the window is
1053 * only increased once per consecutive request received. */
1054 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
1055 !ras->ras_request_index)
1056 ras_increase_window(inode, ras, ra);
1060 ras->ras_request_index++;
1061 ras->ras_last_read_end = pos + PAGE_SIZE - 1;
1062 spin_unlock(&ras->ras_lock);
1066 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1068 struct inode *inode = vmpage->mapping->host;
1069 struct ll_inode_info *lli = ll_i2info(inode);
1072 struct cl_page *page;
1073 struct cl_object *clob;
1074 bool redirtied = false;
1075 bool unlocked = false;
1080 LASSERT(PageLocked(vmpage));
1081 LASSERT(!PageWriteback(vmpage));
1083 LASSERT(ll_i2dtexp(inode) != NULL);
1085 env = cl_env_get(&refcheck);
1087 GOTO(out, result = PTR_ERR(env));
1089 clob = ll_i2info(inode)->lli_clob;
1090 LASSERT(clob != NULL);
1092 io = vvp_env_thread_io(env);
1094 io->ci_ignore_layout = 1;
1095 result = cl_io_init(env, io, CIT_MISC, clob);
1097 page = cl_page_find(env, clob, vmpage->index,
1098 vmpage, CPT_CACHEABLE);
1099 if (!IS_ERR(page)) {
1100 lu_ref_add(&page->cp_reference, "writepage",
1102 cl_page_assume(env, io, page);
1103 result = cl_page_flush(env, io, page);
1106 * Re-dirty page on error so it retries write,
1107 * but not in case when IO has actually
1108 * occurred and completed with an error.
1110 if (!PageError(vmpage)) {
1111 redirty_page_for_writepage(wbc, vmpage);
1116 cl_page_disown(env, io, page);
1118 lu_ref_del(&page->cp_reference,
1119 "writepage", current);
1120 cl_page_put(env, page);
1122 result = PTR_ERR(page);
1125 cl_io_fini(env, io);
1127 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1128 loff_t offset = cl_offset(clob, vmpage->index);
1130 /* Flush page failed because the extent is being written out.
1131 * Wait for the write of extent to be finished to avoid
1132 * breaking kernel which assumes ->writepage should mark
1133 * PageWriteback or clean the page. */
1134 result = cl_sync_file_range(inode, offset,
1135 offset + PAGE_SIZE - 1,
1138 /* actually we may have written more than one page.
1139 * decreasing this page because the caller will count
1141 wbc->nr_to_write -= result - 1;
1146 cl_env_put(env, &refcheck);
1151 if (!lli->lli_async_rc)
1152 lli->lli_async_rc = result;
1153 SetPageError(vmpage);
1155 unlock_page(vmpage);
1160 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1162 struct inode *inode = mapping->host;
1165 enum cl_fsync_mode mode;
1166 int range_whole = 0;
1170 if (wbc->range_cyclic) {
1171 start = mapping->writeback_index << PAGE_SHIFT;
1172 end = OBD_OBJECT_EOF;
1174 start = wbc->range_start;
1175 end = wbc->range_end;
1176 if (end == LLONG_MAX) {
1177 end = OBD_OBJECT_EOF;
1178 range_whole = start == 0;
1182 mode = CL_FSYNC_NONE;
1183 if (wbc->sync_mode == WB_SYNC_ALL)
1184 mode = CL_FSYNC_LOCAL;
1186 if (ll_i2info(inode)->lli_clob == NULL)
1189 /* for directio, it would call writepages() to evict cached pages
1190 * inside the IO context of write, which will cause deadlock at
1191 * layout_conf since it waits for active IOs to complete. */
1192 result = cl_sync_file_range(inode, start, end, mode, 1);
1194 wbc->nr_to_write -= result;
1198 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1199 if (end == OBD_OBJECT_EOF)
1200 mapping->writeback_index = 0;
1202 mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
1207 struct ll_cl_context *ll_cl_find(struct file *file)
1209 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1210 struct ll_cl_context *lcc;
1211 struct ll_cl_context *found = NULL;
1213 read_lock(&fd->fd_lock);
1214 list_for_each_entry(lcc, &fd->fd_lccs, lcc_list) {
1215 if (lcc->lcc_cookie == current) {
1220 read_unlock(&fd->fd_lock);
1225 void ll_cl_add(struct file *file, const struct lu_env *env, struct cl_io *io,
1228 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1229 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1231 memset(lcc, 0, sizeof(*lcc));
1232 INIT_LIST_HEAD(&lcc->lcc_list);
1233 lcc->lcc_cookie = current;
1236 lcc->lcc_type = type;
1238 write_lock(&fd->fd_lock);
1239 list_add(&lcc->lcc_list, &fd->fd_lccs);
1240 write_unlock(&fd->fd_lock);
1243 void ll_cl_remove(struct file *file, const struct lu_env *env)
1245 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1246 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1248 write_lock(&fd->fd_lock);
1249 list_del_init(&lcc->lcc_list);
1250 write_unlock(&fd->fd_lock);
1253 int ll_io_read_page(const struct lu_env *env, struct cl_io *io,
1254 struct cl_page *page, struct file *file)
1256 struct inode *inode = vvp_object_inode(page->cp_obj);
1257 struct ll_sb_info *sbi = ll_i2sbi(inode);
1258 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1259 struct ll_readahead_state *ras = &fd->fd_ras;
1260 struct cl_2queue *queue = &io->ci_queue;
1261 struct cl_sync_io *anchor = NULL;
1262 struct vvp_page *vpg;
1267 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1268 uptodate = vpg->vpg_defer_uptodate;
1270 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1271 sbi->ll_ra_info.ra_max_pages > 0 &&
1272 !vpg->vpg_ra_updated) {
1273 struct vvp_io *vio = vvp_env_io(env);
1274 enum ras_update_flags flags = 0;
1277 flags |= LL_RAS_HIT;
1278 if (!vio->vui_ra_valid)
1279 flags |= LL_RAS_MMAP;
1280 ras_update(sbi, inode, ras, vvp_index(vpg), flags);
1283 cl_2queue_init(queue);
1285 vpg->vpg_ra_used = 1;
1286 cl_page_export(env, page, 1);
1287 cl_page_disown(env, io, page);
1289 anchor = &vvp_env_info(env)->vti_anchor;
1290 cl_sync_io_init(anchor, 1);
1291 page->cp_sync_io = anchor;
1293 cl_2queue_add(queue, page);
1296 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1297 sbi->ll_ra_info.ra_max_pages > 0) {
1300 rc2 = ll_readahead(env, io, &queue->c2_qin, ras,
1302 CDEBUG(D_READA, DFID "%d pages read ahead at %lu\n",
1303 PFID(ll_inode2fid(inode)), rc2, vvp_index(vpg));
1306 if (queue->c2_qin.pl_nr > 0) {
1307 int count = queue->c2_qin.pl_nr;
1308 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
1310 task_io_account_read(PAGE_SIZE * count);
1314 if (anchor != NULL && !cl_page_is_owned(page, io)) { /* have sent */
1315 rc = cl_sync_io_wait(env, anchor, 0);
1317 cl_page_assume(env, io, page);
1318 cl_page_list_del(env, &queue->c2_qout, page);
1320 if (!PageUptodate(cl_page_vmpage(page))) {
1321 /* Failed to read a mirror, discard this page so that
1322 * new page can be created with new mirror.
1324 * TODO: this is not needed after page reinit
1325 * route is implemented */
1326 cl_page_discard(env, io, page);
1328 cl_page_disown(env, io, page);
1331 /* TODO: discard all pages until page reinit route is implemented */
1332 cl_page_list_discard(env, io, &queue->c2_qin);
1334 /* Unlock unsent read pages in case of error. */
1335 cl_page_list_disown(env, io, &queue->c2_qin);
1337 cl_2queue_fini(env, queue);
1343 * Possible return value:
1344 * 0 no async readahead triggered and fast read could not be used.
1345 * 1 no async readahead, but fast read could be used.
1346 * 2 async readahead triggered and fast read could be used too.
1349 static int kickoff_async_readahead(struct file *file, unsigned long pages)
1351 struct ll_readahead_work *lrw;
1352 struct inode *inode = file_inode(file);
1353 struct ll_sb_info *sbi = ll_i2sbi(inode);
1354 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1355 struct ll_readahead_state *ras = &fd->fd_ras;
1356 struct ll_ra_info *ra = &sbi->ll_ra_info;
1357 unsigned long throttle;
1358 unsigned long start = ras_align(ras, ras->ras_next_readahead, NULL);
1359 unsigned long end = start + pages - 1;
1361 throttle = min(ra->ra_async_pages_per_file_threshold,
1362 ra->ra_max_pages_per_file);
1364 * If this is strided i/o or the window is smaller than the
1365 * throttle limit, we do not do async readahead. Otherwise,
1366 * we do async readahead, allowing the user thread to do fast i/o.
1368 if (stride_io_mode(ras) || !throttle ||
1369 ras->ras_window_len < throttle)
1372 if ((atomic_read(&ra->ra_cur_pages) + pages) > ra->ra_max_pages)
1375 if (ras->ras_async_last_readpage == start)
1378 /* ll_readahead_work_free() free it */
1381 lrw->lrw_file = get_file(file);
1382 lrw->lrw_start = start;
1384 spin_lock(&ras->ras_lock);
1385 ras->ras_next_readahead = end + 1;
1386 ras->ras_async_last_readpage = start;
1387 spin_unlock(&ras->ras_lock);
1388 ll_readahead_work_add(inode, lrw);
1396 int ll_readpage(struct file *file, struct page *vmpage)
1398 struct inode *inode = file_inode(file);
1399 struct cl_object *clob = ll_i2info(inode)->lli_clob;
1400 struct ll_cl_context *lcc;
1401 const struct lu_env *env = NULL;
1402 struct cl_io *io = NULL;
1403 struct cl_page *page;
1404 struct ll_sb_info *sbi = ll_i2sbi(inode);
1408 lcc = ll_cl_find(file);
1414 if (io == NULL) { /* fast read */
1415 struct inode *inode = file_inode(file);
1416 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1417 struct ll_readahead_state *ras = &fd->fd_ras;
1418 struct lu_env *local_env = NULL;
1419 unsigned long fast_read_pages =
1420 max(RA_REMAIN_WINDOW_MIN, ras->ras_rpc_size);
1421 struct vvp_page *vpg;
1425 /* TODO: need to verify the layout version to make sure
1426 * the page is not invalid due to layout change. */
1427 page = cl_vmpage_page(vmpage, clob);
1429 unlock_page(vmpage);
1430 ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1434 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1435 if (vpg->vpg_defer_uptodate) {
1436 enum ras_update_flags flags = LL_RAS_HIT;
1438 if (lcc && lcc->lcc_type == LCC_MMAP)
1439 flags |= LL_RAS_MMAP;
1441 /* For fast read, it updates read ahead state only
1442 * if the page is hit in cache because non cache page
1443 * case will be handled by slow read later. */
1444 ras_update(sbi, inode, ras, vvp_index(vpg), flags);
1445 /* avoid duplicate ras_update() call */
1446 vpg->vpg_ra_updated = 1;
1448 /* Check if we can issue a readahead RPC, if that is
1449 * the case, we can't do fast IO because we will need
1450 * a cl_io to issue the RPC. */
1451 if (ras->ras_window_start + ras->ras_window_len <
1452 ras->ras_next_readahead + fast_read_pages ||
1453 kickoff_async_readahead(file, fast_read_pages) > 0)
1458 local_env = cl_env_percpu_get();
1462 /* export the page and skip io stack */
1464 vpg->vpg_ra_used = 1;
1465 cl_page_export(env, page, 1);
1467 ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1469 /* release page refcount before unlocking the page to ensure
1470 * the object won't be destroyed in the calling path of
1471 * cl_page_put(). Please see comment in ll_releasepage(). */
1472 cl_page_put(env, page);
1473 unlock_page(vmpage);
1475 cl_env_percpu_put(local_env);
1480 LASSERT(io->ci_state == CIS_IO_GOING);
1481 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
1482 if (!IS_ERR(page)) {
1483 LASSERT(page->cp_type == CPT_CACHEABLE);
1484 if (likely(!PageUptodate(vmpage))) {
1485 cl_page_assume(env, io, page);
1487 result = ll_io_read_page(env, io, page, file);
1489 /* Page from a non-object file. */
1490 unlock_page(vmpage);
1493 cl_page_put(env, page);
1495 unlock_page(vmpage);
1496 result = PTR_ERR(page);