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/
33 * Lustre Lite I/O page cache routines shared by different kernel revs
36 #include <linux/kernel.h>
38 #include <linux/string.h>
39 #include <linux/stat.h>
40 #include <linux/errno.h>
41 #include <linux/unistd.h>
42 #include <linux/writeback.h>
43 #include <asm/uaccess.h>
46 #include <linux/file.h>
47 #include <linux/stat.h>
48 #include <asm/uaccess.h>
50 #include <linux/pagemap.h>
51 /* current_is_kswapd() */
52 #include <linux/swap.h>
53 #include <linux/task_io_accounting_ops.h>
55 #define DEBUG_SUBSYSTEM S_LLITE
57 #include <obd_cksum.h>
58 #include "llite_internal.h"
59 #include <lustre_compat.h>
61 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
64 * Get readahead pages from the filesystem readahead pool of the client for a
67 * /param sbi superblock for filesystem readahead state ll_ra_info
68 * /param ria per-thread readahead state
69 * /param pages number of pages requested for readahead for the thread.
71 * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
72 * It should work well if the ra_max_pages is much greater than the single
73 * file's read-ahead window, and not too many threads contending for
74 * these readahead pages.
76 * TODO: There may be a 'global sync problem' if many threads are trying
77 * to get an ra budget that is larger than the remaining readahead pages
78 * and reach here at exactly the same time. They will compute /a ret to
79 * consume the remaining pages, but will fail at atomic_add_return() and
80 * get a zero ra window, although there is still ra space remaining. - Jay */
82 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
83 struct ra_io_arg *ria,
85 unsigned long pages_min)
87 struct ll_ra_info *ra = &sbi->ll_ra_info;
92 WARN_ON_ONCE(pages_min > pages);
94 * Don't try readahead aggresively if we are limited
95 * LRU pages, otherwise, it could cause deadlock.
97 pages = min(sbi->ll_cache->ccc_lru_max >> 2, pages);
99 * if this happen, we reserve more pages than needed,
100 * this will make us leak @ra_cur_pages, because
101 * ll_ra_count_put() acutally freed @pages.
103 if (unlikely(pages_min > pages))
107 * If read-ahead pages left are less than 1M, do not do read-ahead,
108 * otherwise it will form small read RPC(< 1M), which hurt server
111 ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages),
113 if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages))
116 if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
117 atomic_sub(ret, &ra->ra_cur_pages);
122 if (ret < pages_min) {
123 /* override ra limit for maximum performance */
124 atomic_add(pages_min - ret, &ra->ra_cur_pages);
130 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long pages)
132 struct ll_ra_info *ra = &sbi->ll_ra_info;
133 atomic_sub(pages, &ra->ra_cur_pages);
136 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
138 LASSERTF(which < _NR_RA_STAT, "which: %u\n", which);
139 lprocfs_counter_incr(sbi->ll_ra_stats, which);
142 static inline bool ll_readahead_enabled(struct ll_sb_info *sbi)
144 return sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
145 sbi->ll_ra_info.ra_max_pages > 0;
148 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
150 struct ll_sb_info *sbi = ll_i2sbi(inode);
151 ll_ra_stats_inc_sbi(sbi, which);
154 #define RAS_CDEBUG(ras) \
156 "lre %llu cr %lu cb %llu wsi %lu wp %lu nra %lu rpc %lu " \
157 "r %lu csr %lu so %llu sb %llu sl %llu lr %lu\n", \
158 ras->ras_last_read_end_bytes, ras->ras_consecutive_requests, \
159 ras->ras_consecutive_bytes, ras->ras_window_start_idx, \
160 ras->ras_window_pages, ras->ras_next_readahead_idx, \
161 ras->ras_rpc_pages, ras->ras_requests, \
162 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
163 ras->ras_stride_bytes, ras->ras_stride_length, \
164 ras->ras_async_last_readpage_idx)
166 static bool pos_in_window(loff_t pos, loff_t point,
167 unsigned long before, unsigned long after)
169 loff_t start = point - before;
170 loff_t end = point + after;
177 return start <= pos && pos <= end;
180 enum ll_ra_page_hint {
181 MAYNEED = 0, /* this page possibly accessed soon */
182 WILLNEED /* this page is gurateed to be needed */
186 * Initiates read-ahead of a page with given index.
188 * \retval +ve: page was already uptodate so it will be skipped
190 * \retval -ve: page wasn't added to \a queue for error;
191 * \retval 0: page was added into \a queue for read ahead.
193 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
194 struct cl_page_list *queue, pgoff_t index,
195 enum ll_ra_page_hint hint)
197 struct cl_object *clob = io->ci_obj;
198 struct inode *inode = vvp_object_inode(clob);
199 struct page *vmpage = NULL;
200 struct cl_page *page;
201 struct vvp_page *vpg;
202 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
204 const char *msg = NULL;
210 vmpage = grab_cache_page_nowait(inode->i_mapping, index);
211 if (vmpage == NULL) {
212 which = RA_STAT_FAILED_GRAB_PAGE;
213 msg = "g_c_p_n failed";
214 GOTO(out, rc = -EBUSY);
218 vmpage = find_or_create_page(inode->i_mapping, index,
221 GOTO(out, rc = -ENOMEM);
224 /* should not come here */
225 GOTO(out, rc = -EINVAL);
228 /* Check if vmpage was truncated or reclaimed */
229 if (vmpage->mapping != inode->i_mapping) {
230 which = RA_STAT_WRONG_GRAB_PAGE;
231 msg = "g_c_p_n returned invalid page";
232 GOTO(out, rc = -EBUSY);
235 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
237 which = RA_STAT_FAILED_GRAB_PAGE;
238 msg = "cl_page_find failed";
239 GOTO(out, rc = PTR_ERR(page));
242 lu_ref_add(&page->cp_reference, "ra", current);
243 cl_page_assume(env, io, page);
244 vpg = cl2vvp_page(cl_object_page_slice(clob, page));
245 if (!vpg->vpg_defer_uptodate && !PageUptodate(vmpage)) {
246 if (hint == MAYNEED) {
247 vpg->vpg_defer_uptodate = 1;
248 vpg->vpg_ra_used = 0;
250 cl_page_list_add(queue, page, true);
252 /* skip completed pages */
253 cl_page_unassume(env, io, page);
254 /* This page is already uptodate, returning a positive number
255 * to tell the callers about this */
259 lu_ref_del(&page->cp_reference, "ra", current);
260 cl_page_put(env, page);
263 if (vmpage != NULL) {
268 if (msg != NULL && hint == MAYNEED) {
269 ll_ra_stats_inc(inode, which);
270 CDEBUG(D_READA, "%s\n", msg);
277 #define RIA_DEBUG(ria) \
278 CDEBUG(D_READA, "rs %lu re %lu ro %llu rl %llu rb %llu\n", \
279 ria->ria_start_idx, ria->ria_end_idx, ria->ria_stoff, \
280 ria->ria_length, ria->ria_bytes)
282 static inline int stride_io_mode(struct ll_readahead_state *ras)
284 return ras->ras_consecutive_stride_requests > 1;
287 /* The function calculates how many bytes will be read in
288 * [off, off + length], in such stride IO area,
289 * stride_offset = st_off, stride_lengh = st_len,
290 * stride_bytes = st_bytes
292 * |------------------|*****|------------------|*****|------------|*****|....
295 * |----- st_len -----|
297 * How many bytes it should read in such pattern
298 * |-------------------------------------------------------------|
300 * |<------ length ------->|
302 * = |<----->| + |-------------------------------------| + |---|
303 * start_left st_bytes * i end_left
305 static loff_t stride_byte_count(loff_t st_off, loff_t st_len, loff_t st_bytes,
306 loff_t off, loff_t length)
308 u64 start = off > st_off ? off - st_off : 0;
309 u64 end = off + length > st_off ? off + length - st_off : 0;
314 if (st_len == 0 || length == 0 || end == 0)
317 start = div64_u64_rem(start, st_len, &start_left);
318 if (start_left < st_bytes)
319 start_left = st_bytes - start_left;
323 end = div64_u64_rem(end, st_len, &end_left);
324 if (end_left > st_bytes)
327 CDEBUG(D_READA, "start %llu, end %llu start_left %llu end_left %llu\n",
328 start, end, start_left, end_left);
331 bytes_count = end_left - (st_bytes - start_left);
333 bytes_count = start_left +
334 st_bytes * (end - start - 1) + end_left;
337 "st_off %llu, st_len %llu st_bytes %llu off %llu length %llu bytescount %llu\n",
338 st_off, st_len, st_bytes, off, length, bytes_count);
343 static unsigned long ria_page_count(struct ra_io_arg *ria)
345 loff_t length_bytes = ria->ria_end_idx >= ria->ria_start_idx ?
346 (loff_t)(ria->ria_end_idx -
347 ria->ria_start_idx + 1) << PAGE_SHIFT : 0;
350 if (ria->ria_length > ria->ria_bytes && ria->ria_bytes &&
351 (ria->ria_length & ~PAGE_MASK || ria->ria_bytes & ~PAGE_MASK ||
352 ria->ria_stoff & ~PAGE_MASK)) {
353 /* Over-estimate un-aligned page stride read */
354 unsigned long pg_count = ((ria->ria_bytes +
355 PAGE_SIZE - 1) >> PAGE_SHIFT) + 1;
356 pg_count *= length_bytes / ria->ria_length + 1;
360 bytes_count = stride_byte_count(ria->ria_stoff, ria->ria_length,
362 (loff_t)ria->ria_start_idx<<PAGE_SHIFT,
364 return (bytes_count + PAGE_SIZE - 1) >> PAGE_SHIFT;
367 static pgoff_t ras_align(struct ll_readahead_state *ras, pgoff_t index)
369 unsigned opt_size = min(ras->ras_window_pages, ras->ras_rpc_pages);
373 return index - (index % opt_size);
376 /* Check whether the index is in the defined ra-window */
377 static bool ras_inside_ra_window(pgoff_t idx, struct ra_io_arg *ria)
379 loff_t pos = (loff_t)idx << PAGE_SHIFT;
381 /* If ria_length == ria_bytes, it means non-stride I/O mode,
382 * idx should always inside read-ahead window in this case
383 * For stride I/O mode, just check whether the idx is inside
386 if (ria->ria_length == 0 || ria->ria_length == ria->ria_bytes)
389 if (pos >= ria->ria_stoff) {
392 div64_u64_rem(pos - ria->ria_stoff, ria->ria_length, &offset);
394 if (offset < ria->ria_bytes ||
395 (ria->ria_length - offset) < PAGE_SIZE)
397 } else if (pos + PAGE_SIZE > ria->ria_stoff) {
405 ll_read_ahead_pages(const struct lu_env *env, struct cl_io *io,
406 struct cl_page_list *queue, struct ll_readahead_state *ras,
407 struct ra_io_arg *ria, pgoff_t *ra_end, pgoff_t skip_index)
409 struct cl_read_ahead ra = { 0 };
410 /* busy page count is per stride */
411 int rc = 0, count = 0, busy_page_count = 0;
414 LASSERT(ria != NULL);
417 for (page_idx = ria->ria_start_idx;
418 page_idx <= ria->ria_end_idx && ria->ria_reserved > 0;
420 if (skip_index && page_idx == skip_index)
422 if (ras_inside_ra_window(page_idx, ria)) {
423 if (ra.cra_end_idx == 0 || ra.cra_end_idx < page_idx) {
427 * Do not shrink ria_end_idx at any case until
428 * the minimum end of current read is covered.
430 * Do not extend read lock accross stripe if
431 * lock contention detected.
433 if (ra.cra_contention &&
434 page_idx > ria->ria_end_idx_min) {
435 ria->ria_end_idx = *ra_end;
439 cl_read_ahead_release(env, &ra);
441 rc = cl_io_read_ahead(env, io, page_idx, &ra);
446 * Only shrink ria_end_idx if the matched
447 * LDLM lock doesn't cover more.
449 if (page_idx > ra.cra_end_idx) {
450 ria->ria_end_idx = ra.cra_end_idx;
454 CDEBUG(D_READA, "idx: %lu, ra: %lu, rpc: %lu\n",
455 page_idx, ra.cra_end_idx,
457 LASSERTF(ra.cra_end_idx >= page_idx,
458 "object: %p, indcies %lu / %lu\n",
459 io->ci_obj, ra.cra_end_idx, page_idx);
460 /* update read ahead RPC size.
461 * NB: it's racy but doesn't matter */
462 if (ras->ras_rpc_pages != ra.cra_rpc_pages &&
463 ra.cra_rpc_pages > 0)
464 ras->ras_rpc_pages = ra.cra_rpc_pages;
466 /* trim it to align with optimal RPC size */
467 end_idx = ras_align(ras, ria->ria_end_idx + 1);
468 if (end_idx > 0 && !ria->ria_eof)
469 ria->ria_end_idx = end_idx - 1;
471 if (ria->ria_end_idx < ria->ria_end_idx_min)
472 ria->ria_end_idx = ria->ria_end_idx_min;
474 if (page_idx > ria->ria_end_idx)
477 /* If the page is inside the read-ahead window */
478 rc = ll_read_ahead_page(env, io, queue, page_idx,
480 if (rc < 0 && rc != -EBUSY)
485 "skip busy page: %lu\n", page_idx);
486 /* For page unaligned readahead the first
487 * last pages of each region can be read by
488 * another reader on the same node, and so
489 * may be busy. So only stop for > 2 busy
491 if (busy_page_count > 2)
496 /* Only subtract from reserve & count the page if we
497 * really did readahead on that page. */
502 } else if (stride_io_mode(ras)) {
503 /* If it is not in the read-ahead window, and it is
504 * read-ahead mode, then check whether it should skip
507 loff_t pos = (loff_t)page_idx << PAGE_SHIFT;
510 div64_u64_rem(pos - ria->ria_stoff, ria->ria_length,
512 if (offset >= ria->ria_bytes) {
513 pos += (ria->ria_length - offset);
514 if ((pos >> PAGE_SHIFT) >= page_idx + 1)
515 page_idx = (pos >> PAGE_SHIFT) - 1;
518 "Stride: jump %llu pages to %lu\n",
519 ria->ria_length - offset, page_idx);
525 cl_read_ahead_release(env, &ra);
530 static void ll_readahead_work_free(struct ll_readahead_work *work)
532 fput(work->lrw_file);
536 static void ll_readahead_handle_work(struct work_struct *wq);
537 static void ll_readahead_work_add(struct inode *inode,
538 struct ll_readahead_work *work)
540 INIT_WORK(&work->lrw_readahead_work, ll_readahead_handle_work);
541 queue_work(ll_i2sbi(inode)->ll_ra_info.ll_readahead_wq,
542 &work->lrw_readahead_work);
545 static int ll_readahead_file_kms(const struct lu_env *env,
546 struct cl_io *io, __u64 *kms)
548 struct cl_object *clob;
550 struct cl_attr *attr = vvp_env_thread_attr(env);
554 inode = vvp_object_inode(clob);
556 cl_object_attr_lock(clob);
557 ret = cl_object_attr_get(env, clob, attr);
558 cl_object_attr_unlock(clob);
563 *kms = attr->cat_kms;
567 static void ll_readahead_handle_work(struct work_struct *wq)
569 struct ll_readahead_work *work;
572 struct ra_io_arg *ria;
574 struct ll_file_data *fd;
575 struct ll_readahead_state *ras;
577 struct cl_2queue *queue;
578 pgoff_t ra_end_idx = 0;
579 unsigned long pages, pages_min = 0;
584 struct ll_sb_info *sbi;
586 work = container_of(wq, struct ll_readahead_work,
588 fd = work->lrw_file->private_data;
590 file = work->lrw_file;
591 inode = file_inode(file);
592 sbi = ll_i2sbi(inode);
594 CDEBUG(D_READA|D_IOTRACE,
595 "%s: async ra from %lu to %lu triggered by user pid %d\n",
596 file_dentry(file)->d_name.name, work->lrw_start_idx,
597 work->lrw_end_idx, work->lrw_user_pid);
599 env = cl_env_alloc(&refcheck, LCT_NOREF);
601 GOTO(out_free_work, rc = PTR_ERR(env));
603 io = vvp_env_thread_io(env);
604 ll_io_init(io, file, CIT_READ, NULL);
606 rc = ll_readahead_file_kms(env, io, &kms);
608 GOTO(out_put_env, rc);
611 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
612 GOTO(out_put_env, rc = 0);
615 ria = &ll_env_info(env)->lti_ria;
616 memset(ria, 0, sizeof(*ria));
618 ria->ria_start_idx = work->lrw_start_idx;
619 /* Truncate RA window to end of file */
620 eof_index = (pgoff_t)(kms - 1) >> PAGE_SHIFT;
621 if (eof_index <= work->lrw_end_idx) {
622 work->lrw_end_idx = eof_index;
625 if (work->lrw_end_idx <= work->lrw_start_idx)
626 GOTO(out_put_env, rc = 0);
628 ria->ria_end_idx = work->lrw_end_idx;
629 pages = ria->ria_end_idx - ria->ria_start_idx + 1;
630 ria->ria_reserved = ll_ra_count_get(sbi, ria,
631 ria_page_count(ria), pages_min);
634 "async reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
635 ria->ria_reserved, pages, pages_min,
636 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
637 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
639 if (ria->ria_reserved < pages) {
640 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
641 if (PAGES_TO_MiB(ria->ria_reserved) < 1) {
642 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
643 GOTO(out_put_env, rc = 0);
647 rc = cl_io_rw_init(env, io, CIT_READ, ria->ria_start_idx, pages);
649 GOTO(out_put_env, rc);
651 /* overwrite jobid inited in vvp_io_init() */
652 if (strncmp(ll_i2info(inode)->lli_jobid, work->lrw_jobid,
653 sizeof(work->lrw_jobid)))
654 memcpy(ll_i2info(inode)->lli_jobid, work->lrw_jobid,
655 sizeof(work->lrw_jobid));
657 vvp_env_io(env)->vui_fd = fd;
658 io->ci_state = CIS_LOCKED;
659 io->ci_async_readahead = true;
660 rc = cl_io_start(env, io);
662 GOTO(out_io_fini, rc);
664 queue = &io->ci_queue;
665 cl_2queue_init(queue);
667 rc = ll_read_ahead_pages(env, io, &queue->c2_qin, ras, ria,
669 if (ria->ria_reserved != 0)
670 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
671 if (queue->c2_qin.pl_nr > 0) {
672 int count = queue->c2_qin.pl_nr;
674 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
676 task_io_account_read(PAGE_SIZE * count);
678 if (ria->ria_end_idx == ra_end_idx && ra_end_idx == (kms >> PAGE_SHIFT))
679 ll_ra_stats_inc(inode, RA_STAT_EOF);
681 if (ra_end_idx != ria->ria_end_idx)
682 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
684 /* TODO: discard all pages until page reinit route is implemented */
685 cl_page_list_discard(env, io, &queue->c2_qin);
687 /* Unlock unsent read pages in case of error. */
688 cl_page_list_disown(env, io, &queue->c2_qin);
690 cl_2queue_fini(env, queue);
695 cl_env_put(env, &refcheck);
698 ll_ra_stats_inc_sbi(ll_i2sbi(inode), RA_STAT_ASYNC);
699 atomic_dec(&sbi->ll_ra_info.ra_async_inflight);
700 ll_readahead_work_free(work);
703 static int ll_readahead(const struct lu_env *env, struct cl_io *io,
704 struct cl_page_list *queue,
705 struct ll_readahead_state *ras, bool hit,
706 struct file *file, pgoff_t skip_index)
708 struct vvp_io *vio = vvp_env_io(env);
709 struct ll_thread_info *lti = ll_env_info(env);
710 unsigned long pages, pages_min = 0;
711 pgoff_t ra_end_idx = 0, start_idx = 0, end_idx = 0;
713 struct ra_io_arg *ria = <i->lti_ria;
714 struct cl_object *clob;
717 struct ll_sb_info *sbi;
718 struct ll_ra_info *ra;
725 inode = vvp_object_inode(clob);
726 sbi = ll_i2sbi(inode);
727 ra = &sbi->ll_ra_info;
730 * In case we have a limited max_cached_mb, readahead
731 * should be stopped if it have run out of all LRU slots.
733 if (atomic_read(&ra->ra_cur_pages) >= sbi->ll_cache->ccc_lru_max) {
734 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
738 memset(ria, 0, sizeof(*ria));
739 ret = ll_readahead_file_kms(env, io, &kms);
744 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
748 spin_lock(&ras->ras_lock);
751 * Note: other thread might rollback the ras_next_readahead_idx,
752 * if it can not get the full size of prepared pages, see the
753 * end of this function. For stride read ahead, it needs to
754 * make sure the offset is no less than ras_stride_offset,
755 * so that stride read ahead can work correctly.
757 if (stride_io_mode(ras))
758 start_idx = max_t(pgoff_t, ras->ras_next_readahead_idx,
759 ras->ras_stride_offset >> PAGE_SHIFT);
761 start_idx = ras->ras_next_readahead_idx;
763 if (ras->ras_window_pages > 0)
764 end_idx = ras->ras_window_start_idx + ras->ras_window_pages - 1;
767 end_idx = start_idx + ras->ras_window_pages - 1;
769 /* Enlarge the RA window to encompass the full read */
770 if (vio->vui_ra_valid &&
771 end_idx < vio->vui_ra_start_idx + vio->vui_ra_pages - 1)
772 end_idx = vio->vui_ra_start_idx + vio->vui_ra_pages - 1;
777 /* Truncate RA window to end of file */
778 eof_index = (pgoff_t)((kms - 1) >> PAGE_SHIFT);
779 if (eof_index <= end_idx) {
784 ria->ria_start_idx = start_idx;
785 ria->ria_end_idx = end_idx;
786 /* If stride I/O mode is detected, get stride window*/
787 if (stride_io_mode(ras)) {
788 ria->ria_stoff = ras->ras_stride_offset;
789 ria->ria_length = ras->ras_stride_length;
790 ria->ria_bytes = ras->ras_stride_bytes;
792 spin_unlock(&ras->ras_lock);
795 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
798 pages = ria_page_count(ria);
800 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
805 CDEBUG(D_READA, DFID": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
806 PFID(lu_object_fid(&clob->co_lu)),
807 ria->ria_start_idx, ria->ria_end_idx,
808 vio->vui_ra_valid ? vio->vui_ra_start_idx : 0,
809 vio->vui_ra_valid ? vio->vui_ra_pages : 0,
812 /* at least to extend the readahead window to cover current read */
813 if (!hit && vio->vui_ra_valid &&
814 vio->vui_ra_start_idx + vio->vui_ra_pages > ria->ria_start_idx) {
815 ria->ria_end_idx_min =
816 vio->vui_ra_start_idx + vio->vui_ra_pages - 1;
817 pages_min = vio->vui_ra_start_idx + vio->vui_ra_pages -
820 * For performance reason, exceeding @ra_max_pages
821 * are allowed, but this should be limited with RPC
822 * size in case a large block size read issued. Trim
825 pages_min = min(pages_min, ras->ras_rpc_pages -
826 (ria->ria_start_idx % ras->ras_rpc_pages));
829 /* don't over reserved for mmap range read */
832 if (pages_min > pages)
834 ria->ria_reserved = ll_ra_count_get(ll_i2sbi(inode), ria, pages,
836 if (ria->ria_reserved < pages)
837 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
839 CDEBUG(D_READA, "reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
840 ria->ria_reserved, pages, pages_min,
841 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
842 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
844 ret = ll_read_ahead_pages(env, io, queue, ras, ria, &ra_end_idx,
846 if (ria->ria_reserved != 0)
847 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
849 if (ra_end_idx == end_idx && ra_end_idx == (kms >> PAGE_SHIFT))
850 ll_ra_stats_inc(inode, RA_STAT_EOF);
853 "ra_end_idx = %lu end_idx = %lu stride end = %lu pages = %d\n",
854 ra_end_idx, end_idx, ria->ria_end_idx, ret);
856 if (ra_end_idx != end_idx)
857 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
858 if (ra_end_idx > 0) {
859 /* update the ras so that the next read-ahead tries from
860 * where we left off. */
861 spin_lock(&ras->ras_lock);
862 ras->ras_next_readahead_idx = ra_end_idx + 1;
863 spin_unlock(&ras->ras_lock);
870 static int ll_readpages(const struct lu_env *env, struct cl_io *io,
871 struct cl_page_list *queue,
872 pgoff_t start, pgoff_t end)
881 ret = ll_readahead_file_kms(env, io, &kms);
889 unsigned long end_index;
891 end_index = (unsigned long)((kms - 1) >> PAGE_SHIFT);
892 if (end_index <= end)
896 for (page_idx = start; page_idx <= end; page_idx++) {
897 ret= ll_read_ahead_page(env, io, queue, page_idx,
901 else if (ret == 0) /* ret 1 is already uptodate */
905 RETURN(count > 0 ? count : ret);
908 static void ras_set_start(struct ll_readahead_state *ras, pgoff_t index)
910 ras->ras_window_start_idx = ras_align(ras, index);
913 /* called with the ras_lock held or from places where it doesn't matter */
914 static void ras_reset(struct ll_readahead_state *ras, pgoff_t index)
916 ras->ras_consecutive_requests = 0;
917 ras->ras_consecutive_bytes = 0;
918 ras->ras_window_pages = 0;
919 ras_set_start(ras, index);
920 ras->ras_next_readahead_idx = max(ras->ras_window_start_idx, index + 1);
925 /* called with the ras_lock held or from places where it doesn't matter */
926 static void ras_stride_reset(struct ll_readahead_state *ras)
928 ras->ras_consecutive_stride_requests = 0;
929 ras->ras_stride_length = 0;
930 ras->ras_stride_bytes = 0;
934 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
936 spin_lock_init(&ras->ras_lock);
937 ras->ras_rpc_pages = PTLRPC_MAX_BRW_PAGES;
939 ras->ras_last_read_end_bytes = 0;
940 ras->ras_requests = 0;
941 ras->ras_range_min_start_idx = 0;
942 ras->ras_range_max_end_idx = 0;
943 ras->ras_range_requests = 0;
944 ras->ras_last_range_pages = 0;
948 * Check whether the read request is in the stride window.
949 * If it is in the stride window, return true, otherwise return false.
951 static bool read_in_stride_window(struct ll_readahead_state *ras,
952 loff_t pos, loff_t count)
956 if (ras->ras_stride_length == 0 || ras->ras_stride_bytes == 0 ||
957 ras->ras_stride_bytes == ras->ras_stride_length)
960 stride_gap = pos - ras->ras_last_read_end_bytes - 1;
962 /* If it is contiguous read */
964 return ras->ras_consecutive_bytes + count <=
965 ras->ras_stride_bytes;
967 /* Otherwise check the stride by itself */
968 return (ras->ras_stride_length - ras->ras_stride_bytes) == stride_gap &&
969 ras->ras_consecutive_bytes == ras->ras_stride_bytes &&
970 count <= ras->ras_stride_bytes;
973 static void ras_init_stride_detector(struct ll_readahead_state *ras,
974 loff_t pos, loff_t count)
976 loff_t stride_gap = pos - ras->ras_last_read_end_bytes - 1;
978 LASSERT(ras->ras_consecutive_stride_requests == 0);
980 if (pos <= ras->ras_last_read_end_bytes) {
981 /*Reset stride window for forward read*/
982 ras_stride_reset(ras);
986 ras->ras_stride_bytes = ras->ras_consecutive_bytes;
987 ras->ras_stride_length = stride_gap + ras->ras_consecutive_bytes;
988 ras->ras_consecutive_stride_requests++;
989 ras->ras_stride_offset = pos;
995 stride_page_count(struct ll_readahead_state *ras, loff_t len)
998 stride_byte_count(ras->ras_stride_offset,
999 ras->ras_stride_length, ras->ras_stride_bytes,
1000 ras->ras_window_start_idx << PAGE_SHIFT, len);
1002 return (bytes_count + PAGE_SIZE - 1) >> PAGE_SHIFT;
1005 /* Stride Read-ahead window will be increased inc_len according to
1006 * stride I/O pattern */
1007 static void ras_stride_increase_window(struct ll_readahead_state *ras,
1008 struct ll_ra_info *ra, loff_t inc_bytes)
1010 loff_t window_bytes, stride_bytes;
1015 /* temporarily store in page units to reduce LASSERT() cost below */
1016 end = ras->ras_window_start_idx + ras->ras_window_pages;
1018 LASSERT(ras->ras_stride_length > 0);
1019 LASSERTF(end >= (ras->ras_stride_offset >> PAGE_SHIFT),
1020 "window_start_idx %lu, window_pages %lu stride_offset %llu\n",
1021 ras->ras_window_start_idx, ras->ras_window_pages,
1022 ras->ras_stride_offset);
1025 if (end <= ras->ras_stride_offset)
1028 stride_bytes = end - ras->ras_stride_offset;
1030 div64_u64_rem(stride_bytes, ras->ras_stride_length, &left_bytes);
1031 window_bytes = (ras->ras_window_pages << PAGE_SHIFT);
1032 if (left_bytes < ras->ras_stride_bytes) {
1033 if (ras->ras_stride_bytes - left_bytes >= inc_bytes) {
1034 window_bytes += inc_bytes;
1037 window_bytes += (ras->ras_stride_bytes - left_bytes);
1038 inc_bytes -= (ras->ras_stride_bytes - left_bytes);
1041 window_bytes += (ras->ras_stride_length - left_bytes);
1044 LASSERT(ras->ras_stride_bytes != 0);
1046 step = div64_u64_rem(inc_bytes, ras->ras_stride_bytes, &left_bytes);
1048 window_bytes += step * ras->ras_stride_length + left_bytes;
1049 LASSERT(window_bytes > 0);
1052 if (stride_page_count(ras, window_bytes) <=
1053 ra->ra_max_pages_per_file || ras->ras_window_pages == 0)
1054 ras->ras_window_pages = (window_bytes >> PAGE_SHIFT);
1056 LASSERT(ras->ras_window_pages > 0);
1061 static void ras_increase_window(struct inode *inode,
1062 struct ll_readahead_state *ras,
1063 struct ll_ra_info *ra)
1065 /* The stretch of ra-window should be aligned with max rpc_size
1066 * but current clio architecture does not support retrieve such
1067 * information from lower layer. FIXME later
1069 if (stride_io_mode(ras)) {
1070 ras_stride_increase_window(ras, ra,
1071 (loff_t)ras->ras_rpc_pages << PAGE_SHIFT);
1073 pgoff_t window_pages;
1075 window_pages = min(ras->ras_window_pages + ras->ras_rpc_pages,
1076 ra->ra_max_pages_per_file);
1077 if (window_pages < ras->ras_rpc_pages)
1078 ras->ras_window_pages = window_pages;
1080 ras->ras_window_pages = ras_align(ras, window_pages);
1085 * Seek within 8 pages are considered as sequential read for now.
1087 static inline bool is_loose_seq_read(struct ll_readahead_state *ras, loff_t pos)
1089 return pos_in_window(pos, ras->ras_last_read_end_bytes,
1090 8UL << PAGE_SHIFT, 8UL << PAGE_SHIFT);
1093 static inline bool is_loose_mmap_read(struct ll_sb_info *sbi,
1094 struct ll_readahead_state *ras,
1097 unsigned long range_pages = sbi->ll_ra_info.ra_range_pages;
1099 return pos_in_window(pos, ras->ras_last_read_end_bytes,
1100 range_pages << PAGE_SHIFT,
1101 range_pages << PAGE_SHIFT);
1105 * We have observed slow mmap read performances for some
1106 * applications. The problem is if access pattern is neither
1107 * sequential nor stride, but could be still adjacent in a
1108 * small range and then seek a random position.
1110 * So the pattern could be something like this:
1112 * [1M data] [hole] [0.5M data] [hole] [0.7M data] [1M data]
1115 * Every time an application reads mmap data, it may not only
1116 * read a single 4KB page, but aslo a cluster of nearby pages in
1117 * a range(e.g. 1MB) of the first page after a cache miss.
1119 * The readahead engine is modified to track the range size of
1120 * a cluster of mmap reads, so that after a seek and/or cache miss,
1121 * the range size is used to efficiently prefetch multiple pages
1122 * in a single RPC rather than many small RPCs.
1124 static void ras_detect_cluster_range(struct ll_readahead_state *ras,
1125 struct ll_sb_info *sbi,
1126 unsigned long pos, unsigned long count)
1128 pgoff_t last_pages, pages;
1129 pgoff_t end_idx = (pos + count - 1) >> PAGE_SHIFT;
1131 last_pages = ras->ras_range_max_end_idx -
1132 ras->ras_range_min_start_idx + 1;
1133 /* First time come here */
1134 if (!ras->ras_range_max_end_idx)
1137 /* Random or Stride read */
1138 if (!is_loose_mmap_read(sbi, ras, pos))
1141 ras->ras_range_requests++;
1142 if (ras->ras_range_max_end_idx < end_idx)
1143 ras->ras_range_max_end_idx = end_idx;
1145 if (ras->ras_range_min_start_idx > (pos >> PAGE_SHIFT))
1146 ras->ras_range_min_start_idx = pos >> PAGE_SHIFT;
1148 /* Out of range, consider it as random or stride */
1149 pages = ras->ras_range_max_end_idx -
1150 ras->ras_range_min_start_idx + 1;
1151 if (pages <= sbi->ll_ra_info.ra_range_pages)
1154 ras->ras_last_range_pages = last_pages;
1155 ras->ras_range_requests = 0;
1156 ras->ras_range_min_start_idx = pos >> PAGE_SHIFT;
1157 ras->ras_range_max_end_idx = end_idx;
1160 static void ras_detect_read_pattern(struct ll_readahead_state *ras,
1161 struct ll_sb_info *sbi,
1162 loff_t pos, size_t count, bool mmap)
1164 bool stride_detect = false;
1165 pgoff_t index = pos >> PAGE_SHIFT;
1168 * Reset the read-ahead window in two cases. First when the app seeks
1169 * or reads to some other part of the file. Secondly if we get a
1170 * read-ahead miss that we think we've previously issued. This can
1171 * be a symptom of there being so many read-ahead pages that the VM
1172 * is reclaiming it before we get to it.
1174 if (!is_loose_seq_read(ras, pos)) {
1175 /* Check whether it is in stride I/O mode */
1176 if (!read_in_stride_window(ras, pos, count)) {
1177 if (ras->ras_consecutive_stride_requests == 0)
1178 ras_init_stride_detector(ras, pos, count);
1180 ras_stride_reset(ras);
1181 ras->ras_consecutive_bytes = 0;
1182 ras_reset(ras, index);
1184 ras->ras_consecutive_bytes = 0;
1185 ras->ras_consecutive_requests = 0;
1186 if (++ras->ras_consecutive_stride_requests > 1)
1187 stride_detect = true;
1190 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
1191 } else if (stride_io_mode(ras)) {
1193 * If this is contiguous read but in stride I/O mode
1194 * currently, check whether stride step still is valid,
1195 * if invalid, it will reset the stride ra window to
1198 if (!read_in_stride_window(ras, pos, count)) {
1199 ras_stride_reset(ras);
1200 ras->ras_window_pages = 0;
1201 ras->ras_next_readahead_idx = index;
1205 ras->ras_consecutive_bytes += count;
1207 pgoff_t idx = ras->ras_consecutive_bytes >> PAGE_SHIFT;
1208 unsigned long ra_range_pages =
1209 max_t(unsigned long, RA_MIN_MMAP_RANGE_PAGES,
1210 sbi->ll_ra_info.ra_range_pages);
1212 if ((idx >= ra_range_pages &&
1213 idx % ra_range_pages == 0) || stride_detect)
1214 ras->ras_need_increase_window = true;
1215 } else if ((ras->ras_consecutive_requests > 1 || stride_detect)) {
1216 ras->ras_need_increase_window = true;
1219 ras->ras_last_read_end_bytes = pos + count - 1;
1222 void ll_ras_enter(struct file *f, loff_t pos, size_t count)
1224 struct ll_file_data *fd = f->private_data;
1225 struct ll_readahead_state *ras = &fd->fd_ras;
1226 struct inode *inode = file_inode(f);
1227 unsigned long index = pos >> PAGE_SHIFT;
1228 struct ll_sb_info *sbi = ll_i2sbi(inode);
1230 spin_lock(&ras->ras_lock);
1231 ras->ras_requests++;
1232 ras->ras_consecutive_requests++;
1233 ras->ras_need_increase_window = false;
1234 ras->ras_no_miss_check = false;
1236 * On the second access to a file smaller than the tunable
1237 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
1238 * file up to ra_max_pages_per_file. This is simply a best effort
1239 * and only occurs once per open file. Normal RA behavior is reverted
1240 * to for subsequent IO.
1242 if (ras->ras_requests >= 2) {
1244 struct ll_ra_info *ra = &sbi->ll_ra_info;
1246 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
1249 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
1250 ra->ra_max_read_ahead_whole_pages,
1251 ra->ra_max_pages_per_file);
1254 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
1255 ras->ras_window_start_idx = 0;
1256 ras->ras_next_readahead_idx = index + 1;
1257 ras->ras_window_pages = min(ra->ra_max_pages_per_file,
1258 ra->ra_max_read_ahead_whole_pages);
1259 ras->ras_no_miss_check = true;
1260 GOTO(out_unlock, 0);
1263 ras_detect_read_pattern(ras, sbi, pos, count, false);
1265 spin_unlock(&ras->ras_lock);
1268 static bool index_in_stride_window(struct ll_readahead_state *ras,
1271 loff_t pos = (loff_t)index << PAGE_SHIFT;
1273 if (ras->ras_stride_length == 0 || ras->ras_stride_bytes == 0 ||
1274 ras->ras_stride_bytes == ras->ras_stride_length)
1277 if (pos >= ras->ras_stride_offset) {
1280 div64_u64_rem(pos - ras->ras_stride_offset,
1281 ras->ras_stride_length, &offset);
1282 if (offset < ras->ras_stride_bytes ||
1283 ras->ras_stride_length - offset < PAGE_SIZE)
1285 } else if (ras->ras_stride_offset - pos < PAGE_SIZE) {
1293 * ll_ras_enter() is used to detect read pattern according to pos and count.
1295 * ras_update() is used to detect cache miss and
1296 * reset window or increase window accordingly
1298 static void ras_update(struct ll_sb_info *sbi, struct inode *inode,
1299 struct ll_readahead_state *ras, pgoff_t index,
1300 enum ras_update_flags flags, struct cl_io *io)
1302 struct ll_ra_info *ra = &sbi->ll_ra_info;
1303 bool hit = flags & LL_RAS_HIT;
1306 spin_lock(&ras->ras_lock);
1309 CDEBUG(D_READA|D_IOTRACE, DFID " pages at %lu miss.\n",
1310 PFID(ll_inode2fid(inode)), index);
1311 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
1314 * The readahead window has been expanded to cover whole
1315 * file size, we don't care whether ra miss happen or not.
1316 * Because we will read whole file to page cache even if
1317 * some pages missed.
1319 if (ras->ras_no_miss_check)
1320 GOTO(out_unlock, 0);
1322 if (io && io->ci_rand_read)
1323 GOTO(out_unlock, 0);
1325 if (io && io->ci_seq_read) {
1327 /* to avoid many small read RPC here */
1328 ras->ras_window_pages = sbi->ll_ra_info.ra_range_pages;
1329 ll_ra_stats_inc_sbi(sbi, RA_STAT_MMAP_RANGE_READ);
1334 if (flags & LL_RAS_MMAP) {
1335 unsigned long ra_pages;
1337 ras_detect_cluster_range(ras, sbi, index << PAGE_SHIFT,
1339 ras_detect_read_pattern(ras, sbi, (loff_t)index << PAGE_SHIFT,
1342 /* we did not detect anything but we could prefetch */
1343 if (!ras->ras_need_increase_window &&
1344 ras->ras_window_pages <= sbi->ll_ra_info.ra_range_pages &&
1345 ras->ras_range_requests >= 2) {
1347 ra_pages = max_t(unsigned long,
1348 RA_MIN_MMAP_RANGE_PAGES,
1349 ras->ras_last_range_pages);
1350 if (index < ra_pages / 2)
1353 index -= ra_pages / 2;
1354 ras->ras_window_pages = ra_pages;
1355 ll_ra_stats_inc_sbi(sbi,
1356 RA_STAT_MMAP_RANGE_READ);
1358 ras->ras_window_pages = 0;
1364 if (!hit && ras->ras_window_pages &&
1365 index < ras->ras_next_readahead_idx &&
1366 pos_in_window(index, ras->ras_window_start_idx, 0,
1367 ras->ras_window_pages)) {
1368 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
1369 ras->ras_need_increase_window = false;
1371 if (index_in_stride_window(ras, index) &&
1372 stride_io_mode(ras)) {
1374 * if (index != ras->ras_last_readpage + 1)
1375 * ras->ras_consecutive_pages = 0;
1377 ras_reset(ras, index);
1380 * If stride-RA hit cache miss, the stride
1381 * detector will not be reset to avoid the
1382 * overhead of redetecting read-ahead mode,
1383 * but on the condition that the stride window
1384 * is still intersect with normal sequential
1385 * read-ahead window.
1387 if (ras->ras_window_start_idx < ras->ras_stride_offset)
1388 ras_stride_reset(ras);
1392 * Reset both stride window and normal RA
1395 ras_reset(ras, index);
1396 /* ras->ras_consecutive_pages++; */
1397 ras->ras_consecutive_bytes = 0;
1398 ras_stride_reset(ras);
1399 GOTO(out_unlock, 0);
1404 ras_set_start(ras, index);
1406 if (stride_io_mode(ras)) {
1407 /* Since stride readahead is sentivite to the offset
1408 * of read-ahead, so we use original offset here,
1409 * instead of ras_window_start_idx, which is RPC aligned.
1411 ras->ras_next_readahead_idx = max(index + 1,
1412 ras->ras_next_readahead_idx);
1413 ras->ras_window_start_idx =
1414 max_t(pgoff_t, ras->ras_window_start_idx,
1415 ras->ras_stride_offset >> PAGE_SHIFT);
1417 if (ras->ras_next_readahead_idx < ras->ras_window_start_idx)
1418 ras->ras_next_readahead_idx = ras->ras_window_start_idx;
1420 ras->ras_next_readahead_idx = index + 1;
1423 if (ras->ras_need_increase_window) {
1424 ras_increase_window(inode, ras, ra);
1425 ras->ras_need_increase_window = false;
1430 spin_unlock(&ras->ras_lock);
1433 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1435 struct inode *inode = vmpage->mapping->host;
1436 struct ll_inode_info *lli = ll_i2info(inode);
1439 struct cl_page *page;
1440 struct cl_object *clob;
1441 bool redirtied = false;
1442 bool unlocked = false;
1447 LASSERT(PageLocked(vmpage));
1448 LASSERT(!PageWriteback(vmpage));
1450 LASSERT(ll_i2dtexp(inode) != NULL);
1452 env = cl_env_get(&refcheck);
1454 GOTO(out, result = PTR_ERR(env));
1456 clob = ll_i2info(inode)->lli_clob;
1457 LASSERT(clob != NULL);
1459 io = vvp_env_thread_io(env);
1461 io->ci_ignore_layout = 1;
1462 result = cl_io_init(env, io, CIT_MISC, clob);
1464 page = cl_page_find(env, clob, vmpage->index,
1465 vmpage, CPT_CACHEABLE);
1466 if (!IS_ERR(page)) {
1467 lu_ref_add(&page->cp_reference, "writepage",
1469 cl_page_assume(env, io, page);
1470 result = cl_page_flush(env, io, page);
1473 * Re-dirty page on error so it retries write,
1474 * but not in case when IO has actually
1475 * occurred and completed with an error.
1477 if (!PageError(vmpage)) {
1478 redirty_page_for_writepage(wbc, vmpage);
1483 cl_page_disown(env, io, page);
1485 lu_ref_del(&page->cp_reference,
1486 "writepage", current);
1487 cl_page_put(env, page);
1489 result = PTR_ERR(page);
1492 cl_io_fini(env, io);
1494 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1495 loff_t offset = cl_offset(clob, vmpage->index);
1497 /* Flush page failed because the extent is being written out.
1498 * Wait for the write of extent to be finished to avoid
1499 * breaking kernel which assumes ->writepage should mark
1500 * PageWriteback or clean the page. */
1501 result = cl_sync_file_range(inode, offset,
1502 offset + PAGE_SIZE - 1,
1505 /* actually we may have written more than one page.
1506 * decreasing this page because the caller will count
1508 wbc->nr_to_write -= result - 1;
1513 cl_env_put(env, &refcheck);
1518 if (!lli->lli_async_rc)
1519 lli->lli_async_rc = result;
1520 SetPageError(vmpage);
1522 unlock_page(vmpage);
1527 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1529 struct inode *inode = mapping->host;
1532 enum cl_fsync_mode mode;
1533 int range_whole = 0;
1537 if (wbc->range_cyclic) {
1538 start = (loff_t)mapping->writeback_index << PAGE_SHIFT;
1539 end = OBD_OBJECT_EOF;
1541 start = wbc->range_start;
1542 end = wbc->range_end;
1543 if (end == LLONG_MAX) {
1544 end = OBD_OBJECT_EOF;
1545 range_whole = start == 0;
1549 mode = CL_FSYNC_NONE;
1550 if (wbc->sync_mode == WB_SYNC_ALL)
1551 mode = CL_FSYNC_LOCAL;
1553 if (ll_i2info(inode)->lli_clob == NULL)
1556 /* for directio, it would call writepages() to evict cached pages
1557 * inside the IO context of write, which will cause deadlock at
1558 * layout_conf since it waits for active IOs to complete. */
1559 result = cl_sync_file_range(inode, start, end, mode, 1);
1561 wbc->nr_to_write -= result;
1565 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1566 if (end == OBD_OBJECT_EOF)
1567 mapping->writeback_index = 0;
1569 mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
1574 struct ll_cl_context *ll_cl_find(struct inode *inode)
1576 struct ll_inode_info *lli = ll_i2info(inode);
1577 struct ll_cl_context *lcc;
1578 struct ll_cl_context *found = NULL;
1580 read_lock(&lli->lli_lock);
1581 list_for_each_entry(lcc, &lli->lli_lccs, lcc_list) {
1582 if (lcc->lcc_cookie == current) {
1587 read_unlock(&lli->lli_lock);
1592 void ll_cl_add(struct inode *inode, const struct lu_env *env, struct cl_io *io,
1595 struct ll_inode_info *lli = ll_i2info(inode);
1596 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1598 memset(lcc, 0, sizeof(*lcc));
1599 INIT_LIST_HEAD(&lcc->lcc_list);
1600 lcc->lcc_cookie = current;
1603 lcc->lcc_type = type;
1605 write_lock(&lli->lli_lock);
1606 list_add(&lcc->lcc_list, &lli->lli_lccs);
1607 write_unlock(&lli->lli_lock);
1610 void ll_cl_remove(struct inode *inode, const struct lu_env *env)
1612 struct ll_inode_info *lli = ll_i2info(inode);
1613 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1615 write_lock(&lli->lli_lock);
1616 list_del_init(&lcc->lcc_list);
1617 write_unlock(&lli->lli_lock);
1620 int ll_io_read_page(const struct lu_env *env, struct cl_io *io,
1621 struct cl_page *page, struct file *file)
1623 struct inode *inode = vvp_object_inode(page->cp_obj);
1624 struct ll_sb_info *sbi = ll_i2sbi(inode);
1625 struct ll_file_data *fd = NULL;
1626 struct ll_readahead_state *ras = NULL;
1627 struct cl_2queue *queue = &io->ci_queue;
1628 struct cl_sync_io *anchor = NULL;
1629 struct vvp_page *vpg;
1630 int rc = 0, rc2 = 0;
1632 pgoff_t io_start_index;
1633 pgoff_t io_end_index;
1637 fd = file->private_data;
1641 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1642 uptodate = vpg->vpg_defer_uptodate;
1644 if (ll_readahead_enabled(sbi) && !vpg->vpg_ra_updated && ras) {
1645 struct vvp_io *vio = vvp_env_io(env);
1646 enum ras_update_flags flags = 0;
1649 flags |= LL_RAS_HIT;
1650 if (!vio->vui_ra_valid)
1651 flags |= LL_RAS_MMAP;
1652 ras_update(sbi, inode, ras, vvp_index(vpg), flags, io);
1655 cl_2queue_init(queue);
1657 vpg->vpg_ra_used = 1;
1658 cl_page_export(env, page, 1);
1659 cl_page_disown(env, io, page);
1661 anchor = &vvp_env_info(env)->vti_anchor;
1662 cl_sync_io_init(anchor, 1);
1663 page->cp_sync_io = anchor;
1665 cl_2queue_add(queue, page, true);
1668 io_start_index = cl_index(io->ci_obj, io->u.ci_rw.crw_pos);
1669 io_end_index = cl_index(io->ci_obj, io->u.ci_rw.crw_pos +
1670 io->u.ci_rw.crw_count - 1);
1671 if (ll_readahead_enabled(sbi) && ras && !io->ci_rand_read) {
1672 pgoff_t skip_index = 0;
1674 if (ras->ras_next_readahead_idx < vvp_index(vpg))
1675 skip_index = vvp_index(vpg);
1676 rc2 = ll_readahead(env, io, &queue->c2_qin, ras,
1677 uptodate, file, skip_index);
1678 CDEBUG(D_READA|D_IOTRACE, DFID " %d pages read ahead at %lu\n",
1679 PFID(ll_inode2fid(inode)), rc2, vvp_index(vpg));
1680 } else if (vvp_index(vpg) == io_start_index &&
1681 io_end_index - io_start_index > 0) {
1682 rc2 = ll_readpages(env, io, &queue->c2_qin, io_start_index + 1,
1684 CDEBUG(D_READA, DFID " %d pages read at %lu\n",
1685 PFID(ll_inode2fid(inode)), rc2, vvp_index(vpg));
1688 if (queue->c2_qin.pl_nr > 0) {
1689 int count = queue->c2_qin.pl_nr;
1690 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
1692 task_io_account_read(PAGE_SIZE * count);
1696 if (anchor != NULL && !cl_page_is_owned(page, io)) { /* have sent */
1697 rc = cl_sync_io_wait(env, anchor, 0);
1699 cl_page_assume(env, io, page);
1700 cl_page_list_del(env, &queue->c2_qout, page);
1702 if (!PageUptodate(cl_page_vmpage(page))) {
1703 /* Failed to read a mirror, discard this page so that
1704 * new page can be created with new mirror.
1706 * TODO: this is not needed after page reinit
1707 * route is implemented */
1708 cl_page_discard(env, io, page);
1710 cl_page_disown(env, io, page);
1713 /* TODO: discard all pages until page reinit route is implemented */
1714 cl_page_list_discard(env, io, &queue->c2_qin);
1716 /* Unlock unsent read pages in case of error. */
1717 cl_page_list_disown(env, io, &queue->c2_qin);
1719 cl_2queue_fini(env, queue);
1725 * Possible return value:
1726 * 0 no async readahead triggered and fast read could not be used.
1727 * 1 no async readahead, but fast read could be used.
1728 * 2 async readahead triggered and fast read could be used too.
1731 static int kickoff_async_readahead(struct file *file, unsigned long pages)
1733 struct ll_readahead_work *lrw;
1734 struct inode *inode = file_inode(file);
1735 struct ll_sb_info *sbi = ll_i2sbi(inode);
1736 struct ll_file_data *fd = file->private_data;
1737 struct ll_readahead_state *ras = &fd->fd_ras;
1738 struct ll_ra_info *ra = &sbi->ll_ra_info;
1739 unsigned long throttle;
1740 pgoff_t start_idx = ras_align(ras, ras->ras_next_readahead_idx);
1741 pgoff_t end_idx = start_idx + pages - 1;
1744 * In case we have a limited max_cached_mb, readahead
1745 * should be stopped if it have run out of all LRU slots.
1747 if (atomic_read(&ra->ra_cur_pages) >= sbi->ll_cache->ccc_lru_max) {
1748 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
1752 throttle = min(ra->ra_async_pages_per_file_threshold,
1753 ra->ra_max_pages_per_file);
1755 * If this is strided i/o or the window is smaller than the
1756 * throttle limit, we do not do async readahead. Otherwise,
1757 * we do async readahead, allowing the user thread to do fast i/o.
1759 if (stride_io_mode(ras) || !throttle ||
1760 ras->ras_window_pages < throttle ||
1761 atomic_read(&ra->ra_async_inflight) > ra->ra_async_max_active)
1764 if ((atomic_read(&ra->ra_cur_pages) + pages) > ra->ra_max_pages)
1767 if (ras->ras_async_last_readpage_idx == start_idx)
1770 /* ll_readahead_work_free() free it */
1773 atomic_inc(&sbi->ll_ra_info.ra_async_inflight);
1774 lrw->lrw_file = get_file(file);
1775 lrw->lrw_start_idx = start_idx;
1776 lrw->lrw_end_idx = end_idx;
1777 lrw->lrw_user_pid = current->pid;
1778 spin_lock(&ras->ras_lock);
1779 ras->ras_next_readahead_idx = end_idx + 1;
1780 ras->ras_async_last_readpage_idx = start_idx;
1781 spin_unlock(&ras->ras_lock);
1782 memcpy(lrw->lrw_jobid, ll_i2info(inode)->lli_jobid,
1783 sizeof(lrw->lrw_jobid));
1784 ll_readahead_work_add(inode, lrw);
1793 * Check if we can issue a readahead RPC, if that is
1794 * the case, we can't do fast IO because we will need
1795 * a cl_io to issue the RPC.
1797 static bool ll_use_fast_io(struct file *file,
1798 struct ll_readahead_state *ras, pgoff_t index)
1800 unsigned long fast_read_pages =
1801 max(RA_REMAIN_WINDOW_MIN, ras->ras_rpc_pages);
1803 loff_t stride_bytes = ras->ras_stride_bytes;
1805 if (stride_io_mode(ras) && stride_bytes) {
1806 skip_pages = (ras->ras_stride_length +
1807 ras->ras_stride_bytes - 1) / stride_bytes;
1808 skip_pages *= fast_read_pages;
1810 skip_pages = fast_read_pages;
1813 if (ras->ras_window_start_idx + ras->ras_window_pages <
1814 ras->ras_next_readahead_idx + skip_pages ||
1815 kickoff_async_readahead(file, fast_read_pages) > 0)
1821 int ll_readpage(struct file *file, struct page *vmpage)
1823 struct inode *inode = file_inode(file);
1824 struct cl_object *clob = ll_i2info(inode)->lli_clob;
1825 struct ll_cl_context *lcc;
1826 const struct lu_env *env = NULL;
1827 struct cl_io *io = NULL;
1828 struct cl_page *page;
1829 struct ll_sb_info *sbi = ll_i2sbi(inode);
1833 lcc = ll_cl_find(inode);
1839 if (io == NULL) { /* fast read */
1840 struct inode *inode = file_inode(file);
1841 struct ll_file_data *fd = file->private_data;
1842 struct ll_readahead_state *ras = &fd->fd_ras;
1843 struct lu_env *local_env = NULL;
1844 struct vvp_page *vpg;
1848 /* TODO: need to verify the layout version to make sure
1849 * the page is not invalid due to layout change. */
1850 page = cl_vmpage_page(vmpage, clob);
1852 unlock_page(vmpage);
1853 ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1857 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1858 if (vpg->vpg_defer_uptodate) {
1859 enum ras_update_flags flags = LL_RAS_HIT;
1861 if (lcc && lcc->lcc_type == LCC_MMAP)
1862 flags |= LL_RAS_MMAP;
1864 /* For fast read, it updates read ahead state only
1865 * if the page is hit in cache because non cache page
1866 * case will be handled by slow read later. */
1867 ras_update(sbi, inode, ras, vvp_index(vpg), flags, io);
1868 /* avoid duplicate ras_update() call */
1869 vpg->vpg_ra_updated = 1;
1871 if (ll_use_fast_io(file, ras, vvp_index(vpg)))
1876 local_env = cl_env_percpu_get();
1880 /* export the page and skip io stack */
1882 vpg->vpg_ra_used = 1;
1883 cl_page_export(env, page, 1);
1885 ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1887 /* release page refcount before unlocking the page to ensure
1888 * the object won't be destroyed in the calling path of
1889 * cl_page_put(). Please see comment in ll_releasepage(). */
1890 cl_page_put(env, page);
1891 unlock_page(vmpage);
1893 cl_env_percpu_put(local_env);
1899 * Direct read can fall back to buffered read, but DIO is done
1900 * with lockless i/o, and buffered requires LDLM locking, so in
1901 * this case we must restart without lockless.
1903 if (file->f_flags & O_DIRECT &&
1904 lcc && lcc->lcc_type == LCC_RW &&
1906 unlock_page(vmpage);
1907 io->ci_dio_lock = 1;
1908 io->ci_need_restart = 1;
1912 LASSERT(io->ci_state == CIS_IO_GOING);
1913 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
1914 if (!IS_ERR(page)) {
1915 LASSERT(page->cp_type == CPT_CACHEABLE);
1916 if (likely(!PageUptodate(vmpage))) {
1917 cl_page_assume(env, io, page);
1919 result = ll_io_read_page(env, io, page, file);
1921 /* Page from a non-object file. */
1922 unlock_page(vmpage);
1925 cl_page_put(env, page);
1927 unlock_page(vmpage);
1928 result = PTR_ERR(page);