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);
152 ll_ra_stats_inc_sbi(sbi, which);
155 void ll_ra_stats_add(struct inode *inode, enum ra_stat which, long count)
157 struct ll_sb_info *sbi = ll_i2sbi(inode);
159 LASSERTF(which < _NR_RA_STAT, "which: %u\n", which);
160 lprocfs_counter_add(sbi->ll_ra_stats, which, count);
163 #define RAS_CDEBUG(ras) \
165 "lre %llu cr %lu cb %llu wsi %lu wp %lu nra %lu rpc %lu " \
166 "r %lu csr %lu so %llu sb %llu sl %llu lr %lu\n", \
167 ras->ras_last_read_end_bytes, ras->ras_consecutive_requests, \
168 ras->ras_consecutive_bytes, ras->ras_window_start_idx, \
169 ras->ras_window_pages, ras->ras_next_readahead_idx, \
170 ras->ras_rpc_pages, ras->ras_requests, \
171 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
172 ras->ras_stride_bytes, ras->ras_stride_length, \
173 ras->ras_async_last_readpage_idx)
175 static bool pos_in_window(loff_t pos, loff_t point,
176 unsigned long before, unsigned long after)
178 loff_t start = point - before;
179 loff_t end = point + after;
186 return start <= pos && pos <= end;
189 enum ll_ra_page_hint {
190 MAYNEED = 0, /* this page possibly accessed soon */
191 WILLNEED /* this page is gurateed to be needed */
195 * Initiates read-ahead of a page with given index.
197 * \retval +ve: page was already uptodate so it will be skipped
199 * \retval -ve: page wasn't added to \a queue for error;
200 * \retval 0: page was added into \a queue for read ahead.
202 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
203 struct cl_page_list *queue, pgoff_t index,
204 enum ll_ra_page_hint hint)
206 struct cl_object *clob = io->ci_obj;
207 struct inode *inode = vvp_object_inode(clob);
208 struct page *vmpage = NULL;
210 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
212 const char *msg = NULL;
219 * We need __GFP_NORETRY here for read-ahead page, otherwise
220 * the process will fail with OOM killed due to memcg limit.
221 * See @readahead_gfp_mask for an example.
223 vmpage = pagecache_get_page(inode->i_mapping, index,
224 FGP_LOCK | FGP_CREAT |
225 FGP_NOFS | FGP_NOWAIT,
226 mapping_gfp_mask(inode->i_mapping) |
227 __GFP_NORETRY | __GFP_NOWARN);
228 if (vmpage == NULL) {
229 which = RA_STAT_FAILED_GRAB_PAGE;
230 msg = "g_c_p_n failed";
231 GOTO(out, rc = -EBUSY);
235 vmpage = find_or_create_page(inode->i_mapping, index,
238 GOTO(out, rc = -ENOMEM);
241 /* should not come here */
242 GOTO(out, rc = -EINVAL);
245 /* Check if vmpage was truncated or reclaimed */
246 if (vmpage->mapping != inode->i_mapping) {
247 which = RA_STAT_WRONG_GRAB_PAGE;
248 msg = "g_c_p_n returned invalid page";
249 GOTO(out, rc = -EBUSY);
252 cp = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
254 which = RA_STAT_FAILED_GRAB_PAGE;
255 msg = "cl_page_find failed";
256 GOTO(out, rc = PTR_ERR(cp));
259 lu_ref_add(&cp->cp_reference, "ra", current);
260 cl_page_assume(env, io, cp);
262 if (!cp->cp_defer_uptodate && !PageUptodate(vmpage)) {
263 if (hint == MAYNEED) {
264 cp->cp_defer_uptodate = 1;
268 cl_page_list_add(queue, cp, true);
270 /* skip completed pages */
271 cl_page_unassume(env, io, cp);
272 /* This page is already uptodate, returning a positive number
273 * to tell the callers about this */
277 lu_ref_del(&cp->cp_reference, "ra", current);
278 cl_page_put(env, cp);
281 if (vmpage != NULL) {
286 if (msg != NULL && hint == MAYNEED) {
287 ll_ra_stats_inc(inode, which);
288 CDEBUG(D_READA, "%s\n", msg);
295 #define RIA_DEBUG(ria) \
296 CDEBUG(D_READA, "rs %lu re %lu ro %llu rl %llu rb %llu\n", \
297 ria->ria_start_idx, ria->ria_end_idx, ria->ria_stoff, \
298 ria->ria_length, ria->ria_bytes)
300 static inline int stride_io_mode(struct ll_readahead_state *ras)
302 return ras->ras_consecutive_stride_requests > 1;
305 /* The function calculates how many bytes will be read in
306 * [off, off + length], in such stride IO area,
307 * stride_offset = st_off, stride_lengh = st_len,
308 * stride_bytes = st_bytes
310 * |------------------|*****|------------------|*****|------------|*****|....
313 * |----- st_len -----|
315 * How many bytes it should read in such pattern
316 * |-------------------------------------------------------------|
318 * |<------ length ------->|
320 * = |<----->| + |-------------------------------------| + |---|
321 * start_left st_bytes * i end_left
323 static loff_t stride_byte_count(loff_t st_off, loff_t st_len, loff_t st_bytes,
324 loff_t off, loff_t length)
326 u64 start = off > st_off ? off - st_off : 0;
327 u64 end = off + length > st_off ? off + length - st_off : 0;
332 if (st_len == 0 || length == 0 || end == 0)
335 start = div64_u64_rem(start, st_len, &start_left);
336 if (start_left < st_bytes)
337 start_left = st_bytes - start_left;
341 end = div64_u64_rem(end, st_len, &end_left);
342 if (end_left > st_bytes)
345 CDEBUG(D_READA, "start %llu, end %llu start_left %llu end_left %llu\n",
346 start, end, start_left, end_left);
349 bytes_count = end_left - (st_bytes - start_left);
351 bytes_count = start_left +
352 st_bytes * (end - start - 1) + end_left;
355 "st_off %llu, st_len %llu st_bytes %llu off %llu length %llu bytescount %llu\n",
356 st_off, st_len, st_bytes, off, length, bytes_count);
361 static unsigned long ria_page_count(struct ra_io_arg *ria)
363 loff_t length_bytes = ria->ria_end_idx >= ria->ria_start_idx ?
364 (loff_t)(ria->ria_end_idx -
365 ria->ria_start_idx + 1) << PAGE_SHIFT : 0;
368 if (ria->ria_length > ria->ria_bytes && ria->ria_bytes &&
369 (ria->ria_length & ~PAGE_MASK || ria->ria_bytes & ~PAGE_MASK ||
370 ria->ria_stoff & ~PAGE_MASK)) {
371 /* Over-estimate un-aligned page stride read */
372 unsigned long pg_count = ((ria->ria_bytes +
373 PAGE_SIZE - 1) >> PAGE_SHIFT) + 1;
374 pg_count *= length_bytes / ria->ria_length + 1;
378 bytes_count = stride_byte_count(ria->ria_stoff, ria->ria_length,
380 (loff_t)ria->ria_start_idx<<PAGE_SHIFT,
382 return (bytes_count + PAGE_SIZE - 1) >> PAGE_SHIFT;
385 static pgoff_t ras_align(struct ll_readahead_state *ras, pgoff_t index)
387 unsigned opt_size = min(ras->ras_window_pages, ras->ras_rpc_pages);
391 return index - (index % opt_size);
394 /* Check whether the index is in the defined ra-window */
395 static bool ras_inside_ra_window(pgoff_t idx, struct ra_io_arg *ria)
397 loff_t pos = (loff_t)idx << PAGE_SHIFT;
399 /* If ria_length == ria_bytes, it means non-stride I/O mode,
400 * idx should always inside read-ahead window in this case
401 * For stride I/O mode, just check whether the idx is inside
404 if (ria->ria_length == 0 || ria->ria_length == ria->ria_bytes)
407 if (pos >= ria->ria_stoff) {
410 div64_u64_rem(pos - ria->ria_stoff, ria->ria_length, &offset);
412 if (offset < ria->ria_bytes ||
413 (ria->ria_length - offset) < PAGE_SIZE)
415 } else if (pos + PAGE_SIZE > ria->ria_stoff) {
423 ll_read_ahead_pages(const struct lu_env *env, struct cl_io *io,
424 struct cl_page_list *queue, struct ll_readahead_state *ras,
425 struct ra_io_arg *ria, pgoff_t *ra_end, pgoff_t skip_index)
427 struct cl_read_ahead ra = { 0 };
428 /* busy page count is per stride */
429 int rc = 0, count = 0, busy_page_count = 0;
432 LASSERT(ria != NULL);
435 for (page_idx = ria->ria_start_idx;
436 page_idx <= ria->ria_end_idx && ria->ria_reserved > 0;
438 if (skip_index && page_idx == skip_index)
440 if (ras_inside_ra_window(page_idx, ria)) {
441 if (ra.cra_end_idx == 0 || ra.cra_end_idx < page_idx) {
445 * Do not shrink ria_end_idx at any case until
446 * the minimum end of current read is covered.
448 * Do not extend read lock accross stripe if
449 * lock contention detected.
451 if (ra.cra_contention &&
452 page_idx > ria->ria_end_idx_min) {
453 ria->ria_end_idx = *ra_end;
457 cl_read_ahead_release(env, &ra);
459 rc = cl_io_read_ahead(env, io, page_idx, &ra);
464 * Only shrink ria_end_idx if the matched
465 * LDLM lock doesn't cover more.
467 if (page_idx > ra.cra_end_idx) {
468 ria->ria_end_idx = ra.cra_end_idx;
472 CDEBUG(D_READA, "idx: %lu, ra: %lu, rpc: %lu\n",
473 page_idx, ra.cra_end_idx,
475 LASSERTF(ra.cra_end_idx >= page_idx,
476 "object: %p, indcies %lu / %lu\n",
477 io->ci_obj, ra.cra_end_idx, page_idx);
478 /* update read ahead RPC size.
479 * NB: it's racy but doesn't matter */
480 if (ras->ras_rpc_pages != ra.cra_rpc_pages &&
481 ra.cra_rpc_pages > 0)
482 ras->ras_rpc_pages = ra.cra_rpc_pages;
484 /* trim it to align with optimal RPC size */
485 end_idx = ras_align(ras, ria->ria_end_idx + 1);
486 if (end_idx > 0 && !ria->ria_eof)
487 ria->ria_end_idx = end_idx - 1;
489 if (ria->ria_end_idx < ria->ria_end_idx_min)
490 ria->ria_end_idx = ria->ria_end_idx_min;
492 if (page_idx > ria->ria_end_idx)
495 /* If the page is inside the read-ahead window */
496 rc = ll_read_ahead_page(env, io, queue, page_idx,
498 if (rc < 0 && rc != -EBUSY)
503 "skip busy page: %lu\n", page_idx);
504 /* For page unaligned readahead the first
505 * last pages of each region can be read by
506 * another reader on the same node, and so
507 * may be busy. So only stop for > 2 busy
509 if (busy_page_count > 2)
514 /* Only subtract from reserve & count the page if we
515 * really did readahead on that page. */
520 } else if (stride_io_mode(ras)) {
521 /* If it is not in the read-ahead window, and it is
522 * read-ahead mode, then check whether it should skip
525 loff_t pos = (loff_t)page_idx << PAGE_SHIFT;
528 div64_u64_rem(pos - ria->ria_stoff, ria->ria_length,
530 if (offset >= ria->ria_bytes) {
531 pos += (ria->ria_length - offset);
532 if ((pos >> PAGE_SHIFT) >= page_idx + 1)
533 page_idx = (pos >> PAGE_SHIFT) - 1;
536 "Stride: jump %llu pages to %lu\n",
537 ria->ria_length - offset, page_idx);
543 cl_read_ahead_release(env, &ra);
546 ll_ra_stats_add(vvp_object_inode(io->ci_obj),
547 RA_STAT_READAHEAD_PAGES, count);
552 static void ll_readahead_work_free(struct ll_readahead_work *work)
554 fput(work->lrw_file);
558 static void ll_readahead_handle_work(struct work_struct *wq);
559 static void ll_readahead_work_add(struct inode *inode,
560 struct ll_readahead_work *work)
562 INIT_WORK(&work->lrw_readahead_work, ll_readahead_handle_work);
563 queue_work(ll_i2sbi(inode)->ll_ra_info.ll_readahead_wq,
564 &work->lrw_readahead_work);
567 static int ll_readahead_file_kms(const struct lu_env *env,
568 struct cl_io *io, __u64 *kms)
570 struct cl_object *clob;
572 struct cl_attr *attr = vvp_env_thread_attr(env);
576 inode = vvp_object_inode(clob);
578 cl_object_attr_lock(clob);
579 ret = cl_object_attr_get(env, clob, attr);
580 cl_object_attr_unlock(clob);
585 *kms = attr->cat_kms;
589 static void ll_readahead_handle_work(struct work_struct *wq)
591 struct ll_readahead_work *work;
594 struct ra_io_arg *ria;
596 struct ll_file_data *fd;
597 struct ll_readahead_state *ras;
599 struct cl_2queue *queue;
600 pgoff_t ra_end_idx = 0;
601 unsigned long pages, pages_min = 0;
606 struct ll_sb_info *sbi;
608 work = container_of(wq, struct ll_readahead_work,
610 fd = work->lrw_file->private_data;
612 file = work->lrw_file;
613 inode = file_inode(file);
614 sbi = ll_i2sbi(inode);
616 CDEBUG(D_READA|D_IOTRACE,
617 "%s:"DFID": async ra from %lu to %lu triggered by user pid %d\n",
618 file_dentry(file)->d_name.name, PFID(ll_inode2fid(inode)),
619 work->lrw_start_idx, work->lrw_end_idx, work->lrw_user_pid);
621 env = cl_env_alloc(&refcheck, LCT_NOREF);
623 GOTO(out_free_work, rc = PTR_ERR(env));
625 io = vvp_env_thread_io(env);
626 ll_io_init(io, file, CIT_READ, NULL);
628 rc = ll_readahead_file_kms(env, io, &kms);
630 GOTO(out_put_env, rc);
633 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
634 GOTO(out_put_env, rc = 0);
637 ria = &ll_env_info(env)->lti_ria;
638 memset(ria, 0, sizeof(*ria));
640 ria->ria_start_idx = work->lrw_start_idx;
641 /* Truncate RA window to end of file */
642 eof_index = (pgoff_t)(kms - 1) >> PAGE_SHIFT;
643 if (eof_index <= work->lrw_end_idx) {
644 work->lrw_end_idx = eof_index;
647 if (work->lrw_end_idx <= work->lrw_start_idx)
648 GOTO(out_put_env, rc = 0);
650 ria->ria_end_idx = work->lrw_end_idx;
651 pages = ria->ria_end_idx - ria->ria_start_idx + 1;
652 ria->ria_reserved = ll_ra_count_get(sbi, ria,
653 ria_page_count(ria), pages_min);
656 "async reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
657 ria->ria_reserved, pages, pages_min,
658 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
659 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
661 if (ria->ria_reserved < pages) {
662 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
663 if (PAGES_TO_MiB(ria->ria_reserved) < 1) {
664 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
665 GOTO(out_put_env, rc = 0);
669 rc = cl_io_rw_init(env, io, CIT_READ, ria->ria_start_idx, pages);
671 GOTO(out_put_env, rc);
673 /* overwrite jobid inited in vvp_io_init() */
674 if (strncmp(ll_i2info(inode)->lli_jobid, work->lrw_jobid,
675 sizeof(work->lrw_jobid)))
676 memcpy(ll_i2info(inode)->lli_jobid, work->lrw_jobid,
677 sizeof(work->lrw_jobid));
679 vvp_env_io(env)->vui_fd = fd;
680 io->ci_state = CIS_LOCKED;
681 io->ci_async_readahead = true;
682 rc = cl_io_start(env, io);
684 GOTO(out_io_fini, rc);
686 queue = &io->ci_queue;
687 cl_2queue_init(queue);
689 rc = ll_read_ahead_pages(env, io, &queue->c2_qin, ras, ria,
691 if (ria->ria_reserved != 0)
692 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
693 if (queue->c2_qin.pl_nr > 0) {
694 int count = queue->c2_qin.pl_nr;
696 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
698 task_io_account_read(PAGE_SIZE * count);
700 if (ria->ria_end_idx == ra_end_idx && ra_end_idx == (kms >> PAGE_SHIFT))
701 ll_ra_stats_inc(inode, RA_STAT_EOF);
703 if (ra_end_idx != ria->ria_end_idx)
704 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
706 /* TODO: discard all pages until page reinit route is implemented */
707 cl_page_list_discard(env, io, &queue->c2_qin);
709 /* Unlock unsent read pages in case of error. */
710 cl_page_list_disown(env, &queue->c2_qin);
712 cl_2queue_fini(env, queue);
717 cl_env_put(env, &refcheck);
720 ll_ra_stats_inc_sbi(ll_i2sbi(inode), RA_STAT_ASYNC);
721 atomic_dec(&sbi->ll_ra_info.ra_async_inflight);
722 ll_readahead_work_free(work);
725 static int ll_readahead(const struct lu_env *env, struct cl_io *io,
726 struct cl_page_list *queue,
727 struct ll_readahead_state *ras, bool hit,
728 struct file *file, pgoff_t skip_index,
731 struct vvp_io *vio = vvp_env_io(env);
732 struct ll_thread_info *lti = ll_env_info(env);
733 unsigned long pages, pages_min = 0;
734 pgoff_t ra_end_idx = 0, end_idx = 0;
736 struct ra_io_arg *ria = <i->lti_ria;
737 struct cl_object *clob;
740 struct ll_sb_info *sbi;
741 struct ll_ra_info *ra;
746 inode = vvp_object_inode(clob);
747 sbi = ll_i2sbi(inode);
748 ra = &sbi->ll_ra_info;
751 * In case we have a limited max_cached_mb, readahead
752 * should be stopped if it have run out of all LRU slots.
754 if (atomic_read(&ra->ra_cur_pages) >= sbi->ll_cache->ccc_lru_max) {
755 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
759 memset(ria, 0, sizeof(*ria));
760 ret = ll_readahead_file_kms(env, io, &kms);
765 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
769 spin_lock(&ras->ras_lock);
772 * Note: other thread might rollback the ras_next_readahead_idx,
773 * if it can not get the full size of prepared pages, see the
774 * end of this function. For stride read ahead, it needs to
775 * make sure the offset is no less than ras_stride_offset,
776 * so that stride read ahead can work correctly.
778 if (stride_io_mode(ras))
779 *start_idx = max_t(pgoff_t, ras->ras_next_readahead_idx,
780 ras->ras_stride_offset >> PAGE_SHIFT);
782 *start_idx = ras->ras_next_readahead_idx;
784 if (ras->ras_window_pages > 0)
785 end_idx = ras->ras_window_start_idx + ras->ras_window_pages - 1;
788 end_idx = *start_idx + ras->ras_window_pages - 1;
790 /* Enlarge the RA window to encompass the full read */
791 if (vio->vui_ra_valid &&
792 end_idx < vio->vui_ra_start_idx + vio->vui_ra_pages - 1)
793 end_idx = vio->vui_ra_start_idx + vio->vui_ra_pages - 1;
798 /* Truncate RA window to end of file */
799 eof_index = (pgoff_t)((kms - 1) >> PAGE_SHIFT);
800 if (eof_index <= end_idx) {
805 ria->ria_start_idx = *start_idx;
806 ria->ria_end_idx = end_idx;
807 /* If stride I/O mode is detected, get stride window*/
808 if (stride_io_mode(ras)) {
809 ria->ria_stoff = ras->ras_stride_offset;
810 ria->ria_length = ras->ras_stride_length;
811 ria->ria_bytes = ras->ras_stride_bytes;
813 spin_unlock(&ras->ras_lock);
816 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
819 pages = ria_page_count(ria);
821 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
826 CDEBUG(D_READA, DFID": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
827 PFID(lu_object_fid(&clob->co_lu)),
828 ria->ria_start_idx, ria->ria_end_idx,
829 vio->vui_ra_valid ? vio->vui_ra_start_idx : 0,
830 vio->vui_ra_valid ? vio->vui_ra_pages : 0,
833 /* at least to extend the readahead window to cover current read */
834 if (!hit && vio->vui_ra_valid &&
835 vio->vui_ra_start_idx + vio->vui_ra_pages > ria->ria_start_idx) {
836 ria->ria_end_idx_min =
837 vio->vui_ra_start_idx + vio->vui_ra_pages - 1;
838 pages_min = vio->vui_ra_start_idx + vio->vui_ra_pages -
841 * For performance reason, exceeding @ra_max_pages
842 * are allowed, but this should be limited with RPC
843 * size in case a large block size read issued. Trim
846 pages_min = min(pages_min, ras->ras_rpc_pages -
847 (ria->ria_start_idx % ras->ras_rpc_pages));
850 /* don't over reserved for mmap range read */
853 if (pages_min > pages)
855 ria->ria_reserved = ll_ra_count_get(ll_i2sbi(inode), ria, pages,
857 if (ria->ria_reserved < pages)
858 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
860 CDEBUG(D_READA, "reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
861 ria->ria_reserved, pages, pages_min,
862 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
863 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
865 ret = ll_read_ahead_pages(env, io, queue, ras, ria, &ra_end_idx,
867 if (ria->ria_reserved != 0)
868 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
870 if (ra_end_idx == end_idx && ra_end_idx == (kms >> PAGE_SHIFT))
871 ll_ra_stats_inc(inode, RA_STAT_EOF);
874 "ra_end_idx = %lu end_idx = %lu stride end = %lu pages = %d\n",
875 ra_end_idx, end_idx, ria->ria_end_idx, ret);
877 if (ra_end_idx != end_idx)
878 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
879 if (ra_end_idx > 0) {
880 /* update the ras so that the next read-ahead tries from
881 * where we left off. */
882 spin_lock(&ras->ras_lock);
883 ras->ras_next_readahead_idx = ra_end_idx + 1;
884 spin_unlock(&ras->ras_lock);
891 static int ll_readpages(const struct lu_env *env, struct cl_io *io,
892 struct cl_page_list *queue,
893 pgoff_t start, pgoff_t end)
902 ret = ll_readahead_file_kms(env, io, &kms);
910 unsigned long end_index;
912 end_index = (unsigned long)((kms - 1) >> PAGE_SHIFT);
913 if (end_index <= end)
917 for (page_idx = start; page_idx <= end; page_idx++) {
918 ret= ll_read_ahead_page(env, io, queue, page_idx,
922 else if (ret == 0) /* ret 1 is already uptodate */
926 RETURN(count > 0 ? count : ret);
929 static void ras_set_start(struct ll_readahead_state *ras, pgoff_t index)
931 ras->ras_window_start_idx = ras_align(ras, index);
934 /* called with the ras_lock held or from places where it doesn't matter */
935 static void ras_reset(struct ll_readahead_state *ras, pgoff_t index)
937 ras->ras_consecutive_requests = 0;
938 ras->ras_consecutive_bytes = 0;
939 ras->ras_window_pages = 0;
940 ras_set_start(ras, index);
941 ras->ras_next_readahead_idx = max(ras->ras_window_start_idx, index + 1);
946 /* called with the ras_lock held or from places where it doesn't matter */
947 static void ras_stride_reset(struct ll_readahead_state *ras)
949 ras->ras_consecutive_stride_requests = 0;
950 ras->ras_stride_length = 0;
951 ras->ras_stride_bytes = 0;
955 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
957 spin_lock_init(&ras->ras_lock);
958 ras->ras_rpc_pages = PTLRPC_MAX_BRW_PAGES;
960 ras->ras_last_read_end_bytes = 0;
961 ras->ras_requests = 0;
962 ras->ras_range_min_start_idx = 0;
963 ras->ras_range_max_end_idx = 0;
964 ras->ras_range_requests = 0;
965 ras->ras_last_range_pages = 0;
969 * Check whether the read request is in the stride window.
970 * If it is in the stride window, return true, otherwise return false.
972 static bool read_in_stride_window(struct ll_readahead_state *ras,
973 loff_t pos, loff_t count)
977 if (ras->ras_stride_length == 0 || ras->ras_stride_bytes == 0 ||
978 ras->ras_stride_bytes == ras->ras_stride_length)
981 stride_gap = pos - ras->ras_last_read_end_bytes - 1;
983 /* If it is contiguous read */
985 return ras->ras_consecutive_bytes + count <=
986 ras->ras_stride_bytes;
988 /* Otherwise check the stride by itself */
989 return (ras->ras_stride_length - ras->ras_stride_bytes) == stride_gap &&
990 ras->ras_consecutive_bytes == ras->ras_stride_bytes &&
991 count <= ras->ras_stride_bytes;
994 static void ras_init_stride_detector(struct ll_readahead_state *ras,
995 loff_t pos, loff_t count)
997 loff_t stride_gap = pos - ras->ras_last_read_end_bytes - 1;
999 LASSERT(ras->ras_consecutive_stride_requests == 0);
1001 if (pos <= ras->ras_last_read_end_bytes) {
1002 /*Reset stride window for forward read*/
1003 ras_stride_reset(ras);
1007 ras->ras_stride_bytes = ras->ras_consecutive_bytes;
1008 ras->ras_stride_length = stride_gap + ras->ras_consecutive_bytes;
1009 ras->ras_consecutive_stride_requests++;
1010 ras->ras_stride_offset = pos;
1015 static unsigned long
1016 stride_page_count(struct ll_readahead_state *ras, loff_t len)
1018 loff_t bytes_count =
1019 stride_byte_count(ras->ras_stride_offset,
1020 ras->ras_stride_length, ras->ras_stride_bytes,
1021 ras->ras_window_start_idx << PAGE_SHIFT, len);
1023 return (bytes_count + PAGE_SIZE - 1) >> PAGE_SHIFT;
1026 /* Stride Read-ahead window will be increased inc_len according to
1027 * stride I/O pattern */
1028 static void ras_stride_increase_window(struct ll_readahead_state *ras,
1029 struct ll_ra_info *ra, loff_t inc_bytes)
1031 loff_t window_bytes, stride_bytes;
1036 /* temporarily store in page units to reduce LASSERT() cost below */
1037 end = ras->ras_window_start_idx + ras->ras_window_pages;
1039 LASSERT(ras->ras_stride_length > 0);
1040 LASSERTF(end >= (ras->ras_stride_offset >> PAGE_SHIFT),
1041 "window_start_idx %lu, window_pages %lu stride_offset %llu\n",
1042 ras->ras_window_start_idx, ras->ras_window_pages,
1043 ras->ras_stride_offset);
1046 if (end <= ras->ras_stride_offset)
1049 stride_bytes = end - ras->ras_stride_offset;
1051 div64_u64_rem(stride_bytes, ras->ras_stride_length, &left_bytes);
1052 window_bytes = (ras->ras_window_pages << PAGE_SHIFT);
1053 if (left_bytes < ras->ras_stride_bytes) {
1054 if (ras->ras_stride_bytes - left_bytes >= inc_bytes) {
1055 window_bytes += inc_bytes;
1058 window_bytes += (ras->ras_stride_bytes - left_bytes);
1059 inc_bytes -= (ras->ras_stride_bytes - left_bytes);
1062 window_bytes += (ras->ras_stride_length - left_bytes);
1065 LASSERT(ras->ras_stride_bytes != 0);
1067 step = div64_u64_rem(inc_bytes, ras->ras_stride_bytes, &left_bytes);
1069 window_bytes += step * ras->ras_stride_length + left_bytes;
1070 LASSERT(window_bytes > 0);
1073 if (stride_page_count(ras, window_bytes) <=
1074 ra->ra_max_pages_per_file || ras->ras_window_pages == 0)
1075 ras->ras_window_pages = (window_bytes >> PAGE_SHIFT);
1077 LASSERT(ras->ras_window_pages > 0);
1082 static void ras_increase_window(struct inode *inode,
1083 struct ll_readahead_state *ras,
1084 struct ll_ra_info *ra)
1086 /* The stretch of ra-window should be aligned with max rpc_size
1087 * but current clio architecture does not support retrieve such
1088 * information from lower layer. FIXME later
1090 if (stride_io_mode(ras)) {
1091 ras_stride_increase_window(ras, ra,
1092 (loff_t)ras->ras_rpc_pages << PAGE_SHIFT);
1094 pgoff_t window_pages;
1096 window_pages = min(ras->ras_window_pages + ras->ras_rpc_pages,
1097 ra->ra_max_pages_per_file);
1098 if (window_pages < ras->ras_rpc_pages)
1099 ras->ras_window_pages = window_pages;
1101 ras->ras_window_pages = ras_align(ras, window_pages);
1106 * Seek within 8 pages are considered as sequential read for now.
1108 static inline bool is_loose_seq_read(struct ll_readahead_state *ras, loff_t pos)
1110 return pos_in_window(pos, ras->ras_last_read_end_bytes,
1111 8UL << PAGE_SHIFT, 8UL << PAGE_SHIFT);
1114 static inline bool is_loose_mmap_read(struct ll_sb_info *sbi,
1115 struct ll_readahead_state *ras,
1118 unsigned long range_pages = sbi->ll_ra_info.ra_range_pages;
1120 return pos_in_window(pos, ras->ras_last_read_end_bytes,
1121 range_pages << PAGE_SHIFT,
1122 range_pages << PAGE_SHIFT);
1126 * We have observed slow mmap read performances for some
1127 * applications. The problem is if access pattern is neither
1128 * sequential nor stride, but could be still adjacent in a
1129 * small range and then seek a random position.
1131 * So the pattern could be something like this:
1133 * [1M data] [hole] [0.5M data] [hole] [0.7M data] [1M data]
1136 * Every time an application reads mmap data, it may not only
1137 * read a single 4KB page, but aslo a cluster of nearby pages in
1138 * a range(e.g. 1MB) of the first page after a cache miss.
1140 * The readahead engine is modified to track the range size of
1141 * a cluster of mmap reads, so that after a seek and/or cache miss,
1142 * the range size is used to efficiently prefetch multiple pages
1143 * in a single RPC rather than many small RPCs.
1145 static void ras_detect_cluster_range(struct ll_readahead_state *ras,
1146 struct ll_sb_info *sbi,
1147 unsigned long pos, unsigned long count)
1149 pgoff_t last_pages, pages;
1150 pgoff_t end_idx = (pos + count - 1) >> PAGE_SHIFT;
1152 last_pages = ras->ras_range_max_end_idx -
1153 ras->ras_range_min_start_idx + 1;
1154 /* First time come here */
1155 if (!ras->ras_range_max_end_idx)
1158 /* Random or Stride read */
1159 if (!is_loose_mmap_read(sbi, ras, pos))
1162 ras->ras_range_requests++;
1163 if (ras->ras_range_max_end_idx < end_idx)
1164 ras->ras_range_max_end_idx = end_idx;
1166 if (ras->ras_range_min_start_idx > (pos >> PAGE_SHIFT))
1167 ras->ras_range_min_start_idx = pos >> PAGE_SHIFT;
1169 /* Out of range, consider it as random or stride */
1170 pages = ras->ras_range_max_end_idx -
1171 ras->ras_range_min_start_idx + 1;
1172 if (pages <= sbi->ll_ra_info.ra_range_pages)
1175 ras->ras_last_range_pages = last_pages;
1176 ras->ras_range_requests = 0;
1177 ras->ras_range_min_start_idx = pos >> PAGE_SHIFT;
1178 ras->ras_range_max_end_idx = end_idx;
1181 static void ras_detect_read_pattern(struct ll_readahead_state *ras,
1182 struct ll_sb_info *sbi,
1183 loff_t pos, size_t count, bool mmap)
1185 bool stride_detect = false;
1186 pgoff_t index = pos >> PAGE_SHIFT;
1189 * Reset the read-ahead window in two cases. First when the app seeks
1190 * or reads to some other part of the file. Secondly if we get a
1191 * read-ahead miss that we think we've previously issued. This can
1192 * be a symptom of there being so many read-ahead pages that the VM
1193 * is reclaiming it before we get to it.
1195 if (!is_loose_seq_read(ras, pos)) {
1196 /* Check whether it is in stride I/O mode */
1197 if (!read_in_stride_window(ras, pos, count)) {
1198 if (ras->ras_consecutive_stride_requests == 0)
1199 ras_init_stride_detector(ras, pos, count);
1201 ras_stride_reset(ras);
1202 ras->ras_consecutive_bytes = 0;
1203 ras_reset(ras, index);
1205 ras->ras_consecutive_bytes = 0;
1206 ras->ras_consecutive_requests = 0;
1207 if (++ras->ras_consecutive_stride_requests > 1)
1208 stride_detect = true;
1211 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
1212 } else if (stride_io_mode(ras)) {
1214 * If this is contiguous read but in stride I/O mode
1215 * currently, check whether stride step still is valid,
1216 * if invalid, it will reset the stride ra window to
1219 if (!read_in_stride_window(ras, pos, count)) {
1220 ras_stride_reset(ras);
1221 ras->ras_window_pages = 0;
1222 ras->ras_next_readahead_idx = index;
1226 ras->ras_consecutive_bytes += count;
1228 pgoff_t idx = ras->ras_consecutive_bytes >> PAGE_SHIFT;
1229 unsigned long ra_range_pages =
1230 max_t(unsigned long, RA_MIN_MMAP_RANGE_PAGES,
1231 sbi->ll_ra_info.ra_range_pages);
1233 if ((idx >= ra_range_pages &&
1234 idx % ra_range_pages == 0) || stride_detect)
1235 ras->ras_need_increase_window = true;
1236 } else if ((ras->ras_consecutive_requests > 1 || stride_detect)) {
1237 ras->ras_need_increase_window = true;
1240 ras->ras_last_read_end_bytes = pos + count - 1;
1243 void ll_ras_enter(struct file *f, loff_t pos, size_t count)
1245 struct ll_file_data *fd = f->private_data;
1246 struct ll_readahead_state *ras = &fd->fd_ras;
1247 struct inode *inode = file_inode(f);
1248 unsigned long index = pos >> PAGE_SHIFT;
1249 struct ll_sb_info *sbi = ll_i2sbi(inode);
1251 spin_lock(&ras->ras_lock);
1252 ras->ras_requests++;
1253 ras->ras_consecutive_requests++;
1254 ras->ras_need_increase_window = false;
1255 ras->ras_no_miss_check = false;
1257 * On the second access to a file smaller than the tunable
1258 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
1259 * file up to ra_max_pages_per_file. This is simply a best effort
1260 * and only occurs once per open file. Normal RA behavior is reverted
1261 * to for subsequent IO.
1263 if (ras->ras_requests >= 2) {
1265 struct ll_ra_info *ra = &sbi->ll_ra_info;
1267 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
1270 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
1271 ra->ra_max_read_ahead_whole_pages,
1272 ra->ra_max_pages_per_file);
1275 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
1276 ras->ras_window_start_idx = 0;
1277 ras->ras_next_readahead_idx = index + 1;
1278 ras->ras_window_pages = min(ra->ra_max_pages_per_file,
1279 ra->ra_max_read_ahead_whole_pages);
1280 ras->ras_no_miss_check = true;
1281 GOTO(out_unlock, 0);
1284 ras_detect_read_pattern(ras, sbi, pos, count, false);
1286 spin_unlock(&ras->ras_lock);
1289 static bool index_in_stride_window(struct ll_readahead_state *ras,
1292 loff_t pos = (loff_t)index << PAGE_SHIFT;
1294 if (ras->ras_stride_length == 0 || ras->ras_stride_bytes == 0 ||
1295 ras->ras_stride_bytes == ras->ras_stride_length)
1298 if (pos >= ras->ras_stride_offset) {
1301 div64_u64_rem(pos - ras->ras_stride_offset,
1302 ras->ras_stride_length, &offset);
1303 if (offset < ras->ras_stride_bytes ||
1304 ras->ras_stride_length - offset < PAGE_SIZE)
1306 } else if (ras->ras_stride_offset - pos < PAGE_SIZE) {
1314 * ll_ras_enter() is used to detect read pattern according to pos and count.
1316 * ras_update() is used to detect cache miss and
1317 * reset window or increase window accordingly
1319 static void ras_update(struct ll_sb_info *sbi, struct inode *inode,
1320 struct ll_readahead_state *ras, pgoff_t index,
1321 enum ras_update_flags flags, struct cl_io *io)
1323 struct ll_ra_info *ra = &sbi->ll_ra_info;
1324 bool hit = flags & LL_RAS_HIT;
1327 spin_lock(&ras->ras_lock);
1330 CDEBUG(D_READA|D_IOTRACE, DFID " pages at %lu miss.\n",
1331 PFID(ll_inode2fid(inode)), index);
1332 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
1335 * The readahead window has been expanded to cover whole
1336 * file size, we don't care whether ra miss happen or not.
1337 * Because we will read whole file to page cache even if
1338 * some pages missed.
1340 if (ras->ras_no_miss_check)
1341 GOTO(out_unlock, 0);
1343 if (io && io->ci_rand_read)
1344 GOTO(out_unlock, 0);
1346 if (io && io->ci_seq_read) {
1348 /* to avoid many small read RPC here */
1349 ras->ras_window_pages = sbi->ll_ra_info.ra_range_pages;
1350 ll_ra_stats_inc_sbi(sbi, RA_STAT_MMAP_RANGE_READ);
1355 if (flags & LL_RAS_MMAP) {
1356 unsigned long ra_pages;
1358 ras_detect_cluster_range(ras, sbi, index << PAGE_SHIFT,
1360 ras_detect_read_pattern(ras, sbi, (loff_t)index << PAGE_SHIFT,
1363 /* we did not detect anything but we could prefetch */
1364 if (!ras->ras_need_increase_window &&
1365 ras->ras_window_pages <= sbi->ll_ra_info.ra_range_pages &&
1366 ras->ras_range_requests >= 2) {
1368 ra_pages = max_t(unsigned long,
1369 RA_MIN_MMAP_RANGE_PAGES,
1370 ras->ras_last_range_pages);
1371 if (index < ra_pages / 2)
1374 index -= ra_pages / 2;
1375 ras->ras_window_pages = ra_pages;
1376 ll_ra_stats_inc_sbi(sbi,
1377 RA_STAT_MMAP_RANGE_READ);
1379 ras->ras_window_pages = 0;
1385 if (!hit && ras->ras_window_pages &&
1386 index < ras->ras_next_readahead_idx &&
1387 pos_in_window(index, ras->ras_window_start_idx, 0,
1388 ras->ras_window_pages)) {
1389 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
1390 ras->ras_need_increase_window = false;
1392 if (index_in_stride_window(ras, index) &&
1393 stride_io_mode(ras)) {
1395 * if (index != ras->ras_last_readpage + 1)
1396 * ras->ras_consecutive_pages = 0;
1398 ras_reset(ras, index);
1401 * If stride-RA hit cache miss, the stride
1402 * detector will not be reset to avoid the
1403 * overhead of redetecting read-ahead mode,
1404 * but on the condition that the stride window
1405 * is still intersect with normal sequential
1406 * read-ahead window.
1408 if (ras->ras_window_start_idx < ras->ras_stride_offset)
1409 ras_stride_reset(ras);
1413 * Reset both stride window and normal RA
1416 ras_reset(ras, index);
1417 /* ras->ras_consecutive_pages++; */
1418 ras->ras_consecutive_bytes = 0;
1419 ras_stride_reset(ras);
1420 GOTO(out_unlock, 0);
1425 ras_set_start(ras, index);
1427 if (stride_io_mode(ras)) {
1428 /* Since stride readahead is sentivite to the offset
1429 * of read-ahead, so we use original offset here,
1430 * instead of ras_window_start_idx, which is RPC aligned.
1432 ras->ras_next_readahead_idx = max(index + 1,
1433 ras->ras_next_readahead_idx);
1434 ras->ras_window_start_idx =
1435 max_t(pgoff_t, ras->ras_window_start_idx,
1436 ras->ras_stride_offset >> PAGE_SHIFT);
1438 if (ras->ras_next_readahead_idx < ras->ras_window_start_idx)
1439 ras->ras_next_readahead_idx = ras->ras_window_start_idx;
1441 ras->ras_next_readahead_idx = index + 1;
1444 if (ras->ras_need_increase_window) {
1445 ras_increase_window(inode, ras, ra);
1446 ras->ras_need_increase_window = false;
1451 spin_unlock(&ras->ras_lock);
1454 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1456 struct inode *inode = vmpage->mapping->host;
1457 struct ll_inode_info *lli = ll_i2info(inode);
1460 struct cl_page *page;
1461 struct cl_object *clob;
1462 bool redirtied = false;
1463 bool unlocked = false;
1468 LASSERT(PageLocked(vmpage));
1469 LASSERT(!PageWriteback(vmpage));
1471 LASSERT(ll_i2dtexp(inode) != NULL);
1473 env = cl_env_get(&refcheck);
1475 GOTO(out, result = PTR_ERR(env));
1477 clob = ll_i2info(inode)->lli_clob;
1478 LASSERT(clob != NULL);
1480 io = vvp_env_thread_io(env);
1482 io->ci_ignore_layout = 1;
1483 result = cl_io_init(env, io, CIT_MISC, clob);
1485 page = cl_page_find(env, clob, vmpage->index,
1486 vmpage, CPT_CACHEABLE);
1487 if (!IS_ERR(page)) {
1488 lu_ref_add(&page->cp_reference, "writepage",
1490 cl_page_assume(env, io, page);
1491 result = cl_page_flush(env, io, page);
1494 * Re-dirty page on error so it retries write,
1495 * but not in case when IO has actually
1496 * occurred and completed with an error.
1498 if (!PageError(vmpage)) {
1499 redirty_page_for_writepage(wbc, vmpage);
1504 cl_page_disown(env, io, page);
1506 lu_ref_del(&page->cp_reference,
1507 "writepage", current);
1508 cl_page_put(env, page);
1510 result = PTR_ERR(page);
1513 cl_io_fini(env, io);
1515 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1516 loff_t offset = vmpage->index << PAGE_SHIFT;
1518 /* Flush page failed because the extent is being written out.
1519 * Wait for the write of extent to be finished to avoid
1520 * breaking kernel which assumes ->writepage should mark
1521 * PageWriteback or clean the page. */
1522 result = cl_sync_file_range(inode, offset,
1523 offset + PAGE_SIZE - 1,
1526 /* actually we may have written more than one page.
1527 * decreasing this page because the caller will count
1529 wbc->nr_to_write -= result - 1;
1534 cl_env_put(env, &refcheck);
1539 if (!lli->lli_async_rc)
1540 lli->lli_async_rc = result;
1541 SetPageError(vmpage);
1543 unlock_page(vmpage);
1548 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1550 struct inode *inode = mapping->host;
1553 enum cl_fsync_mode mode;
1554 int range_whole = 0;
1558 if (wbc->range_cyclic) {
1559 start = (loff_t)mapping->writeback_index << PAGE_SHIFT;
1560 end = OBD_OBJECT_EOF;
1562 start = wbc->range_start;
1563 end = wbc->range_end;
1564 if (end == LLONG_MAX) {
1565 end = OBD_OBJECT_EOF;
1566 range_whole = start == 0;
1570 mode = CL_FSYNC_NONE;
1571 if (wbc->sync_mode == WB_SYNC_ALL)
1572 mode = CL_FSYNC_LOCAL;
1574 if (ll_i2info(inode)->lli_clob == NULL)
1577 /* for directio, it would call writepages() to evict cached pages
1578 * inside the IO context of write, which will cause deadlock at
1579 * layout_conf since it waits for active IOs to complete. */
1580 result = cl_sync_file_range(inode, start, end, mode, 1);
1582 wbc->nr_to_write -= result;
1586 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1587 if (end == OBD_OBJECT_EOF)
1588 mapping->writeback_index = 0;
1590 mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
1595 struct ll_cl_context *ll_cl_find(struct inode *inode)
1597 struct ll_inode_info *lli = ll_i2info(inode);
1598 struct ll_cl_context *lcc;
1599 struct ll_cl_context *found = NULL;
1601 read_lock(&lli->lli_lock);
1602 list_for_each_entry(lcc, &lli->lli_lccs, lcc_list) {
1603 if (lcc->lcc_cookie == current) {
1608 read_unlock(&lli->lli_lock);
1613 void ll_cl_add(struct inode *inode, const struct lu_env *env, struct cl_io *io,
1616 struct ll_inode_info *lli = ll_i2info(inode);
1617 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1619 memset(lcc, 0, sizeof(*lcc));
1620 INIT_LIST_HEAD(&lcc->lcc_list);
1621 lcc->lcc_cookie = current;
1624 lcc->lcc_type = type;
1626 write_lock(&lli->lli_lock);
1627 list_add(&lcc->lcc_list, &lli->lli_lccs);
1628 write_unlock(&lli->lli_lock);
1631 void ll_cl_remove(struct inode *inode, const struct lu_env *env)
1633 struct ll_inode_info *lli = ll_i2info(inode);
1634 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1636 write_lock(&lli->lli_lock);
1637 list_del_init(&lcc->lcc_list);
1638 write_unlock(&lli->lli_lock);
1641 int ll_io_read_page(const struct lu_env *env, struct cl_io *io,
1642 struct cl_page *page, struct file *file)
1644 struct inode *inode = vvp_object_inode(page->cp_obj);
1645 struct ll_sb_info *sbi = ll_i2sbi(inode);
1646 struct ll_file_data *fd = NULL;
1647 struct ll_readahead_state *ras = NULL;
1648 struct cl_2queue *queue = &io->ci_queue;
1649 struct cl_sync_io *anchor = NULL;
1650 int rc = 0, rc2 = 0;
1652 struct vvp_io *vio = vvp_env_io(env);
1653 bool mmap = !vio->vui_ra_valid;
1654 pgoff_t ra_start_index = 0;
1655 pgoff_t io_start_index;
1656 pgoff_t io_end_index;
1657 bool unlockpage = true;
1661 fd = file->private_data;
1665 /* PagePrivate2 is set in ll_io_zero_page() to tell us the vmpage
1666 * must not be unlocked after processing.
1668 if (page->cp_vmpage && PagePrivate2(page->cp_vmpage))
1671 uptodate = page->cp_defer_uptodate;
1673 if (ll_readahead_enabled(sbi) && !page->cp_ra_updated && ras) {
1674 enum ras_update_flags flags = 0;
1677 flags |= LL_RAS_HIT;
1679 flags |= LL_RAS_MMAP;
1680 ras_update(sbi, inode, ras, cl_page_index(page), flags, io);
1683 cl_2queue_init(queue);
1685 page->cp_ra_used = 1;
1686 SetPageUptodate(page->cp_vmpage);
1687 cl_page_disown(env, io, page);
1689 anchor = &vvp_env_info(env)->vti_anchor;
1690 cl_sync_io_init(anchor, 1);
1691 page->cp_sync_io = anchor;
1693 cl_page_list_add(&queue->c2_qin, page, true);
1696 /* mmap does not set the ci_rw fields */
1698 io_start_index = io->u.ci_rw.crw_pos >> PAGE_SHIFT;
1699 io_end_index = (io->u.ci_rw.crw_pos +
1700 io->u.ci_rw.crw_count - 1) >> PAGE_SHIFT;
1702 io_start_index = cl_page_index(page);
1703 io_end_index = cl_page_index(page);
1706 if (ll_readahead_enabled(sbi) && ras && !io->ci_rand_read) {
1707 pgoff_t skip_index = 0;
1709 if (ras->ras_next_readahead_idx < cl_page_index(page))
1710 skip_index = cl_page_index(page);
1711 rc2 = ll_readahead(env, io, &queue->c2_qin, ras,
1712 uptodate, file, skip_index,
1714 /* to keep iotrace clean, we only print here if we actually
1717 CDEBUG(D_READA | (rc2 ? D_IOTRACE : 0),
1718 DFID " %d pages read ahead at %lu, triggered by user read at %lu, stride offset %lld, stride length %lld, stride bytes %lld\n",
1719 PFID(ll_inode2fid(inode)), rc2, ra_start_index,
1720 cl_page_index(page), ras->ras_stride_offset,
1721 ras->ras_stride_length, ras->ras_stride_bytes);
1723 } else if (cl_page_index(page) == io_start_index &&
1724 io_end_index - io_start_index > 0) {
1725 rc2 = ll_readpages(env, io, &queue->c2_qin, io_start_index + 1,
1727 CDEBUG(D_READA, DFID " %d pages read at %lu\n",
1728 PFID(ll_inode2fid(inode)), rc2, cl_page_index(page));
1731 if (queue->c2_qin.pl_nr > 0) {
1732 int count = queue->c2_qin.pl_nr;
1733 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
1735 task_io_account_read(PAGE_SIZE * count);
1739 if (anchor != NULL && !cl_page_is_owned(page, io)) { /* have sent */
1740 rc = cl_sync_io_wait(env, anchor, 0);
1742 cl_page_assume(env, io, page);
1743 cl_page_list_del(env, &queue->c2_qout, page);
1745 if (!PageUptodate(cl_page_vmpage(page))) {
1746 /* Failed to read a mirror, discard this page so that
1747 * new page can be created with new mirror.
1749 * TODO: this is not needed after page reinit
1750 * route is implemented */
1751 cl_page_discard(env, io, page);
1754 cl_page_disown(env, io, page);
1757 /* TODO: discard all pages until page reinit route is implemented */
1758 cl_page_list_discard(env, io, &queue->c2_qin);
1760 /* Unlock unsent read pages in case of error. */
1761 cl_page_list_disown(env, &queue->c2_qin);
1763 cl_2queue_fini(env, queue);
1769 * Possible return value:
1770 * 0 no async readahead triggered and fast read could not be used.
1771 * 1 no async readahead, but fast read could be used.
1772 * 2 async readahead triggered and fast read could be used too.
1775 static int kickoff_async_readahead(struct file *file, unsigned long pages)
1777 struct ll_readahead_work *lrw;
1778 struct inode *inode = file_inode(file);
1779 struct ll_sb_info *sbi = ll_i2sbi(inode);
1780 struct ll_file_data *fd = file->private_data;
1781 struct ll_readahead_state *ras = &fd->fd_ras;
1782 struct ll_ra_info *ra = &sbi->ll_ra_info;
1783 unsigned long throttle;
1784 pgoff_t start_idx = ras_align(ras, ras->ras_next_readahead_idx);
1785 pgoff_t end_idx = start_idx + pages - 1;
1788 * In case we have a limited max_cached_mb, readahead
1789 * should be stopped if it have run out of all LRU slots.
1791 if (atomic_read(&ra->ra_cur_pages) >= sbi->ll_cache->ccc_lru_max) {
1792 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
1796 throttle = min(ra->ra_async_pages_per_file_threshold,
1797 ra->ra_max_pages_per_file);
1799 * If this is strided i/o or the window is smaller than the
1800 * throttle limit, we do not do async readahead. Otherwise,
1801 * we do async readahead, allowing the user thread to do fast i/o.
1803 if (stride_io_mode(ras) || !throttle ||
1804 ras->ras_window_pages < throttle ||
1805 atomic_read(&ra->ra_async_inflight) > ra->ra_async_max_active)
1808 if ((atomic_read(&ra->ra_cur_pages) + pages) > ra->ra_max_pages)
1811 if (ras->ras_async_last_readpage_idx == start_idx)
1814 /* ll_readahead_work_free() free it */
1817 atomic_inc(&sbi->ll_ra_info.ra_async_inflight);
1818 lrw->lrw_file = get_file(file);
1819 lrw->lrw_start_idx = start_idx;
1820 lrw->lrw_end_idx = end_idx;
1821 lrw->lrw_user_pid = current->pid;
1822 spin_lock(&ras->ras_lock);
1823 ras->ras_next_readahead_idx = end_idx + 1;
1824 ras->ras_async_last_readpage_idx = start_idx;
1825 spin_unlock(&ras->ras_lock);
1826 memcpy(lrw->lrw_jobid, ll_i2info(inode)->lli_jobid,
1827 sizeof(lrw->lrw_jobid));
1828 ll_readahead_work_add(inode, lrw);
1837 * Check if we can issue a readahead RPC, if that is
1838 * the case, we can't do fast IO because we will need
1839 * a cl_io to issue the RPC.
1841 static bool ll_use_fast_io(struct file *file,
1842 struct ll_readahead_state *ras, pgoff_t index)
1844 unsigned long fast_read_pages =
1845 max(RA_REMAIN_WINDOW_MIN, ras->ras_rpc_pages);
1847 loff_t stride_bytes = ras->ras_stride_bytes;
1849 if (stride_io_mode(ras) && stride_bytes) {
1850 skip_pages = (ras->ras_stride_length +
1851 ras->ras_stride_bytes - 1) / stride_bytes;
1852 skip_pages *= fast_read_pages;
1854 skip_pages = fast_read_pages;
1857 if (ras->ras_window_start_idx + ras->ras_window_pages <
1858 ras->ras_next_readahead_idx + skip_pages ||
1859 kickoff_async_readahead(file, fast_read_pages) > 0)
1865 int ll_readpage(struct file *file, struct page *vmpage)
1867 struct inode *inode = file_inode(file);
1868 struct cl_object *clob = ll_i2info(inode)->lli_clob;
1869 struct ll_sb_info *sbi = ll_i2sbi(inode);
1870 const struct lu_env *env = NULL;
1871 struct cl_read_ahead ra = { 0 };
1872 struct ll_cl_context *lcc;
1873 struct cl_io *io = NULL;
1874 struct cl_page *page;
1878 if (CFS_FAIL_PRECHECK(OBD_FAIL_LLITE_READPAGE_PAUSE)) {
1879 unlock_page(vmpage);
1880 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_READPAGE_PAUSE, cfs_fail_val);
1885 * The @vmpage got truncated.
1886 * This is a kernel bug introduced since kernel 5.12:
1887 * comment: cbd59c48ae2bcadc4a7599c29cf32fd3f9b78251
1888 * ("mm/filemap: use head pages in generic_file_buffered_read")
1890 * The page end offset calculation in filemap_get_read_batch() was off
1891 * by one. When a read is submitted with end offset 1048575, then it
1892 * calculates the end page for read of 256 where it should be 255. This
1893 * results in the readpage() for the page with index 256 is over stripe
1894 * boundary and may not covered by a DLM extent lock.
1896 * This happens in a corner race case: filemap_get_read_batch() adds
1897 * the page with index 256 for read which is not in the current read
1898 * I/O context, and this page is being invalidated and will be removed
1899 * from page cache due to the lock protected it being revoken. This
1900 * results in this page in the read path not covered by any DLM lock.
1902 * The solution is simple. Check whether the page was truncated in
1903 * ->readpage(). If so, just return AOP_TRUNCATED_PAGE to the upper
1904 * caller. Then the kernel will retry to batch pages, and it will not
1905 * add the truncated page into batches as it was removed from page
1906 * cache of the file.
1908 if (vmpage->mapping != inode->i_mapping) {
1909 unlock_page(vmpage);
1910 RETURN(AOP_TRUNCATED_PAGE);
1913 lcc = ll_cl_find(inode);
1919 if (io == NULL) { /* fast read */
1920 struct inode *inode = file_inode(file);
1921 struct ll_file_data *fd = file->private_data;
1922 struct ll_readahead_state *ras = &fd->fd_ras;
1923 struct lu_env *local_env = NULL;
1925 CDEBUG(D_VFSTRACE, "fast read pgno: %ld\n", vmpage->index);
1929 /* TODO: need to verify the layout version to make sure
1930 * the page is not invalid due to layout change. */
1931 page = cl_vmpage_page(vmpage, clob);
1933 unlock_page(vmpage);
1934 ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1938 if (page->cp_defer_uptodate) {
1939 enum ras_update_flags flags = LL_RAS_HIT;
1941 if (lcc && lcc->lcc_type == LCC_MMAP)
1942 flags |= LL_RAS_MMAP;
1944 /* For fast read, it updates read ahead state only
1945 * if the page is hit in cache because non cache page
1946 * case will be handled by slow read later. */
1947 ras_update(sbi, inode, ras, cl_page_index(page), flags, io);
1948 /* avoid duplicate ras_update() call */
1949 page->cp_ra_updated = 1;
1951 if (ll_use_fast_io(file, ras, cl_page_index(page)))
1956 local_env = cl_env_percpu_get();
1960 /* export the page and skip io stack */
1962 page->cp_ra_used = 1;
1963 SetPageUptodate(vmpage);
1965 ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1968 /* release page refcount before unlocking the page to ensure
1969 * the object won't be destroyed in the calling path of
1970 * cl_page_put(). Please see comment in ll_releasepage(). */
1971 cl_page_put(env, page);
1972 unlock_page(vmpage);
1974 cl_env_percpu_put(local_env);
1979 if (lcc && lcc->lcc_type != LCC_MMAP) {
1980 CDEBUG(D_VFSTRACE, "pgno:%ld, beyond read end_index:%ld\n",
1981 vmpage->index, lcc->lcc_end_index);
1984 * This handles a kernel bug introduced in kernel 5.12:
1985 * comment: cbd59c48ae2bcadc4a7599c29cf32fd3f9b78251
1986 * ("mm/filemap: use head pages in generic_file_buffered_read")
1988 * See above in this function for a full description of the
1989 * bug. Briefly, the kernel will try to read 1 more page than
1990 * was actually requested *if that page is already in cache*.
1992 * Because this page is beyond the boundary of the requested
1993 * read, Lustre does not lock it as part of the read. This
1994 * means we must check if there is a valid dlmlock on this
1995 * this page and reference it before we attempt to read in the
1996 * page. If there is not a valid dlmlock, then we are racing
1997 * with dlmlock cancellation and the page is being removed
2000 * That means we should return AOP_TRUNCATED_PAGE, which will
2001 * cause the kernel to retry the read, which should allow the
2002 * page to be removed from cache as the lock is cancelled.
2004 * This should never occur except in kernels with the bug
2007 if (vmpage->index >= lcc->lcc_end_index) {
2008 result = cl_io_read_ahead(env, io, vmpage->index, &ra);
2009 if (result < 0 || vmpage->index > ra.cra_end_idx) {
2010 cl_read_ahead_release(env, &ra);
2011 unlock_page(vmpage);
2012 RETURN(AOP_TRUNCATED_PAGE);
2018 * Direct read can fall back to buffered read, but DIO is done
2019 * with lockless i/o, and buffered requires LDLM locking, so in
2020 * this case we must restart without lockless.
2022 if (file->f_flags & O_DIRECT &&
2023 lcc && lcc->lcc_type == LCC_RW &&
2025 unlock_page(vmpage);
2026 io->ci_dio_lock = 1;
2027 io->ci_need_restart = 1;
2028 GOTO(out, result = -ENOLCK);
2031 LASSERT(io->ci_state == CIS_IO_GOING);
2032 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
2033 if (!IS_ERR(page)) {
2034 LASSERT(page->cp_type == CPT_CACHEABLE);
2035 if (likely(!PageUptodate(vmpage))) {
2036 cl_page_assume(env, io, page);
2038 result = ll_io_read_page(env, io, page, file);
2040 /* Page from a non-object file. */
2041 unlock_page(vmpage);
2044 cl_page_put(env, page);
2046 unlock_page(vmpage);
2047 result = PTR_ERR(page);
2051 if (ra.cra_release != NULL)
2052 cl_read_ahead_release(env, &ra);
2054 /* this delay gives time for the actual read of the page to finish and
2055 * unlock the page in vvp_page_completion_read before we return to our
2056 * caller and the caller tries to use the page, allowing us to test
2057 * races with the page being unlocked after readpage() but before it's
2058 * used by the caller
2060 OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_READPAGE_PAUSE2, cfs_fail_val);
2065 #ifdef HAVE_AOPS_READ_FOLIO
2066 int ll_read_folio(struct file *file, struct folio *folio)
2068 return ll_readpage(file, folio_page(folio, 0));