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, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Lustre Lite I/O page cache routines shared by different kernel revs
37 #include <linux/kernel.h>
39 #include <linux/string.h>
40 #include <linux/stat.h>
41 #include <linux/errno.h>
42 #include <linux/unistd.h>
43 #include <linux/writeback.h>
44 #include <linux/uaccess.h>
47 #include <linux/pagemap.h>
48 /* current_is_kswapd() */
49 #include <linux/swap.h>
51 #define DEBUG_SUBSYSTEM S_LLITE
53 #include "../include/obd_cksum.h"
54 #include "llite_internal.h"
56 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
59 * Get readahead pages from the filesystem readahead pool of the client for a
62 * /param sbi superblock for filesystem readahead state ll_ra_info
63 * /param ria per-thread readahead state
64 * /param pages number of pages requested for readahead for the thread.
66 * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
67 * It should work well if the ra_max_pages is much greater than the single
68 * file's read-ahead window, and not too many threads contending for
69 * these readahead pages.
71 * TODO: There may be a 'global sync problem' if many threads are trying
72 * to get an ra budget that is larger than the remaining readahead pages
73 * and reach here at exactly the same time. They will compute /a ret to
74 * consume the remaining pages, but will fail at atomic_add_return() and
75 * get a zero ra window, although there is still ra space remaining. - Jay
77 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
78 struct ra_io_arg *ria,
79 unsigned long pages, unsigned long min)
81 struct ll_ra_info *ra = &sbi->ll_ra_info;
84 /* If read-ahead pages left are less than 1M, do not do read-ahead,
85 * otherwise it will form small read RPC(< 1M), which hurt server
88 ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages), pages);
89 if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages)) {
94 /* If the non-strided (ria_pages == 0) readahead window
95 * (ria_start + ret) has grown across an RPC boundary, then trim
96 * readahead size by the amount beyond the RPC so it ends on an
97 * RPC boundary. If the readahead window is already ending on
98 * an RPC boundary (beyond_rpc == 0), or smaller than a full
99 * RPC (beyond_rpc < ret) the readahead size is unchanged.
100 * The (beyond_rpc != 0) check is skipped since the conditional
101 * branch is more expensive than subtracting zero from the result.
103 * Strided read is left unaligned to avoid small fragments beyond
104 * the RPC boundary from needing an extra read RPC.
106 if (ria->ria_pages == 0) {
107 long beyond_rpc = (ria->ria_start + ret) % PTLRPC_MAX_BRW_PAGES;
109 if (/* beyond_rpc != 0 && */ beyond_rpc < ret)
113 if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
114 atomic_sub(ret, &ra->ra_cur_pages);
120 /* override ra limit for maximum performance */
121 atomic_add(min - ret, &ra->ra_cur_pages);
127 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
129 struct ll_ra_info *ra = &sbi->ll_ra_info;
131 atomic_sub(len, &ra->ra_cur_pages);
134 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
136 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
137 lprocfs_counter_incr(sbi->ll_ra_stats, which);
140 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
142 struct ll_sb_info *sbi = ll_i2sbi(inode);
144 ll_ra_stats_inc_sbi(sbi, which);
147 #define RAS_CDEBUG(ras) \
149 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu" \
150 "csr %lu sf %lu sp %lu sl %lu\n", \
151 ras->ras_last_readpage, ras->ras_consecutive_requests, \
152 ras->ras_consecutive_pages, ras->ras_window_start, \
153 ras->ras_window_len, ras->ras_next_readahead, \
154 ras->ras_requests, ras->ras_request_index, \
155 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
156 ras->ras_stride_pages, ras->ras_stride_length)
158 static int index_in_window(unsigned long index, unsigned long point,
159 unsigned long before, unsigned long after)
161 unsigned long start = point - before, end = point + after;
168 return start <= index && index <= end;
171 void ll_ras_enter(struct file *f)
173 struct ll_file_data *fd = LUSTRE_FPRIVATE(f);
174 struct ll_readahead_state *ras = &fd->fd_ras;
176 spin_lock(&ras->ras_lock);
178 ras->ras_request_index = 0;
179 ras->ras_consecutive_requests++;
180 spin_unlock(&ras->ras_lock);
183 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
184 struct cl_page_list *queue, struct cl_page *page,
185 struct cl_object *clob, pgoff_t *max_index)
187 struct page *vmpage = page->cp_vmpage;
188 struct vvp_page *vpg;
192 cl_page_assume(env, io, page);
193 lu_ref_add(&page->cp_reference, "ra", current);
194 vpg = cl2vvp_page(cl_object_page_slice(clob, page));
195 if (!vpg->vpg_defer_uptodate && !PageUptodate(vmpage)) {
196 CDEBUG(D_READA, "page index %lu, max_index: %lu\n",
197 vvp_index(vpg), *max_index);
198 if (*max_index == 0 || vvp_index(vpg) > *max_index)
199 rc = cl_page_is_under_lock(env, io, page, max_index);
201 vpg->vpg_defer_uptodate = 1;
202 vpg->vpg_ra_used = 0;
203 cl_page_list_add(queue, page);
206 cl_page_discard(env, io, page);
210 /* skip completed pages */
211 cl_page_unassume(env, io, page);
213 lu_ref_del(&page->cp_reference, "ra", current);
214 cl_page_put(env, page);
219 * Initiates read-ahead of a page with given index.
221 * \retval +ve: page was added to \a queue.
223 * \retval -ENOLCK: there is no extent lock for this part of a file, stop
226 * \retval -ve, 0: page wasn't added to \a queue for other reason.
228 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
229 struct cl_page_list *queue,
230 pgoff_t index, pgoff_t *max_index)
232 struct cl_object *clob = io->ci_obj;
233 struct inode *inode = vvp_object_inode(clob);
235 struct cl_page *page;
236 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
238 const char *msg = NULL;
240 vmpage = grab_cache_page_nowait(inode->i_mapping, index);
242 /* Check if vmpage was truncated or reclaimed */
243 if (vmpage->mapping == inode->i_mapping) {
244 page = cl_page_find(env, clob, vmpage->index,
245 vmpage, CPT_CACHEABLE);
247 rc = cl_read_ahead_page(env, io, queue,
248 page, clob, max_index);
250 which = RA_STAT_FAILED_MATCH;
251 msg = "lock match failed";
254 which = RA_STAT_FAILED_GRAB_PAGE;
255 msg = "cl_page_find failed";
258 which = RA_STAT_WRONG_GRAB_PAGE;
259 msg = "g_c_p_n returned invalid page";
265 which = RA_STAT_FAILED_GRAB_PAGE;
266 msg = "g_c_p_n failed";
269 ll_ra_stats_inc(inode, which);
270 CDEBUG(D_READA, "%s\n", msg);
275 #define RIA_DEBUG(ria) \
276 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
277 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
280 /* Limit this to the blocksize instead of PTLRPC_BRW_MAX_SIZE, since we don't
281 * know what the actual RPC size is. If this needs to change, it makes more
282 * sense to tune the i_blkbits value for the file based on the OSTs it is
283 * striped over, rather than having a constant value for all files here.
286 /* RAS_INCREASE_STEP should be (1UL << (inode->i_blkbits - PAGE_SHIFT)).
287 * Temporarily set RAS_INCREASE_STEP to 1MB. After 4MB RPC is enabled
288 * by default, this should be adjusted corresponding with max_read_ahead_mb
289 * and max_read_ahead_per_file_mb otherwise the readahead budget can be used
290 * up quickly which will affect read performance significantly. See LU-2816
292 #define RAS_INCREASE_STEP(inode) (ONE_MB_BRW_SIZE >> PAGE_SHIFT)
294 static inline int stride_io_mode(struct ll_readahead_state *ras)
296 return ras->ras_consecutive_stride_requests > 1;
299 /* The function calculates how much pages will be read in
300 * [off, off + length], in such stride IO area,
301 * stride_offset = st_off, stride_length = st_len,
302 * stride_pages = st_pgs
304 * |------------------|*****|------------------|*****|------------|*****|....
307 * |----- st_len -----|
309 * How many pages it should read in such pattern
310 * |-------------------------------------------------------------|
312 * |<------ length ------->|
314 * = |<----->| + |-------------------------------------| + |---|
315 * start_left st_pgs * i end_left
318 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
319 unsigned long off, unsigned long length)
321 __u64 start = off > st_off ? off - st_off : 0;
322 __u64 end = off + length > st_off ? off + length - st_off : 0;
323 unsigned long start_left = 0;
324 unsigned long end_left = 0;
325 unsigned long pg_count;
327 if (st_len == 0 || length == 0 || end == 0)
330 start_left = do_div(start, st_len);
331 if (start_left < st_pgs)
332 start_left = st_pgs - start_left;
336 end_left = do_div(end, st_len);
337 if (end_left > st_pgs)
340 CDEBUG(D_READA, "start %llu, end %llu start_left %lu end_left %lu\n",
341 start, end, start_left, end_left);
344 pg_count = end_left - (st_pgs - start_left);
346 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
348 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu pgcount %lu\n",
349 st_off, st_len, st_pgs, off, length, pg_count);
354 static int ria_page_count(struct ra_io_arg *ria)
356 __u64 length = ria->ria_end >= ria->ria_start ?
357 ria->ria_end - ria->ria_start + 1 : 0;
359 return stride_pg_count(ria->ria_stoff, ria->ria_length,
360 ria->ria_pages, ria->ria_start,
364 /*Check whether the index is in the defined ra-window */
365 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
367 /* If ria_length == ria_pages, it means non-stride I/O mode,
368 * idx should always inside read-ahead window in this case
369 * For stride I/O mode, just check whether the idx is inside
372 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
373 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
374 ria->ria_length < ria->ria_pages);
377 static int ll_read_ahead_pages(const struct lu_env *env,
378 struct cl_io *io, struct cl_page_list *queue,
379 struct ra_io_arg *ria,
380 unsigned long *reserved_pages,
381 unsigned long *ra_end)
386 pgoff_t max_index = 0;
391 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
392 for (page_idx = ria->ria_start;
393 page_idx <= ria->ria_end && *reserved_pages > 0; page_idx++) {
394 if (ras_inside_ra_window(page_idx, ria)) {
395 /* If the page is inside the read-ahead window*/
396 rc = ll_read_ahead_page(env, io, queue,
397 page_idx, &max_index);
401 } else if (rc == -ENOLCK) {
404 } else if (stride_ria) {
405 /* If it is not in the read-ahead window, and it is
406 * read-ahead mode, then check whether it should skip
410 /* FIXME: This assertion only is valid when it is for
411 * forward read-ahead, it will be fixed when backward
412 * read-ahead is implemented
414 LASSERTF(page_idx >= ria->ria_stoff, "Invalid page_idx %lu rs %lu re %lu ro %lu rl %lu rp %lu\n",
416 ria->ria_start, ria->ria_end, ria->ria_stoff,
417 ria->ria_length, ria->ria_pages);
418 offset = page_idx - ria->ria_stoff;
419 offset = offset % (ria->ria_length);
420 if (offset > ria->ria_pages) {
421 page_idx += ria->ria_length - offset;
422 CDEBUG(D_READA, "i %lu skip %lu\n", page_idx,
423 ria->ria_length - offset);
432 int ll_readahead(const struct lu_env *env, struct cl_io *io,
433 struct cl_page_list *queue, struct ll_readahead_state *ras,
436 struct vvp_io *vio = vvp_env_io(env);
437 struct ll_thread_info *lti = ll_env_info(env);
438 struct cl_attr *attr = vvp_env_thread_attr(env);
439 unsigned long start = 0, end = 0, reserved;
440 unsigned long ra_end, len, mlen = 0;
442 struct ra_io_arg *ria = <i->lti_ria;
443 struct cl_object *clob;
448 inode = vvp_object_inode(clob);
450 memset(ria, 0, sizeof(*ria));
452 cl_object_attr_lock(clob);
453 ret = cl_object_attr_get(env, clob, attr);
454 cl_object_attr_unlock(clob);
460 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
464 spin_lock(&ras->ras_lock);
466 /* Enlarge the RA window to encompass the full read */
467 if (vio->vui_ra_valid &&
468 ras->ras_window_start + ras->ras_window_len <
469 vio->vui_ra_start + vio->vui_ra_count) {
470 ras->ras_window_len = vio->vui_ra_start + vio->vui_ra_count -
471 ras->ras_window_start;
474 /* Reserve a part of the read-ahead window that we'll be issuing */
475 if (ras->ras_window_len > 0) {
477 * Note: other thread might rollback the ras_next_readahead,
478 * if it can not get the full size of prepared pages, see the
479 * end of this function. For stride read ahead, it needs to
480 * make sure the offset is no less than ras_stride_offset,
481 * so that stride read ahead can work correctly.
483 if (stride_io_mode(ras))
484 start = max(ras->ras_next_readahead,
485 ras->ras_stride_offset);
487 start = ras->ras_next_readahead;
488 end = ras->ras_window_start + ras->ras_window_len - 1;
492 unsigned long rpc_boundary;
494 * Align RA window to an optimal boundary.
496 * XXX This would be better to align to cl_max_pages_per_rpc
497 * instead of PTLRPC_MAX_BRW_PAGES, because the RPC size may
498 * be aligned to the RAID stripe size in the future and that
499 * is more important than the RPC size.
501 /* Note: we only trim the RPC, instead of extending the RPC
502 * to the boundary, so to avoid reading too much pages during
505 rpc_boundary = (end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1));
506 if (rpc_boundary > 0)
509 if (rpc_boundary > start)
512 /* Truncate RA window to end of file */
513 end = min(end, (unsigned long)((kms - 1) >> PAGE_SHIFT));
515 ras->ras_next_readahead = max(end, end + 1);
518 ria->ria_start = start;
520 /* If stride I/O mode is detected, get stride window*/
521 if (stride_io_mode(ras)) {
522 ria->ria_stoff = ras->ras_stride_offset;
523 ria->ria_length = ras->ras_stride_length;
524 ria->ria_pages = ras->ras_stride_pages;
526 spin_unlock(&ras->ras_lock);
529 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
532 len = ria_page_count(ria);
534 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
538 CDEBUG(D_READA, DFID ": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
539 PFID(lu_object_fid(&clob->co_lu)),
540 ria->ria_start, ria->ria_end,
541 vio->vui_ra_valid ? vio->vui_ra_start : 0,
542 vio->vui_ra_valid ? vio->vui_ra_count : 0,
545 /* at least to extend the readahead window to cover current read */
546 if (!hit && vio->vui_ra_valid &&
547 vio->vui_ra_start + vio->vui_ra_count > ria->ria_start) {
548 /* to the end of current read window. */
549 mlen = vio->vui_ra_start + vio->vui_ra_count - ria->ria_start;
550 /* trim to RPC boundary */
551 start = ria->ria_start & (PTLRPC_MAX_BRW_PAGES - 1);
552 mlen = min(mlen, PTLRPC_MAX_BRW_PAGES - start);
555 reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len, mlen);
557 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
559 CDEBUG(D_READA, "reserved pages %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
561 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
562 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
564 ret = ll_read_ahead_pages(env, io, queue, ria, &reserved, &ra_end);
567 ll_ra_count_put(ll_i2sbi(inode), reserved);
569 if (ra_end == end + 1 && ra_end == (kms >> PAGE_SHIFT))
570 ll_ra_stats_inc(inode, RA_STAT_EOF);
572 /* if we didn't get to the end of the region we reserved from
573 * the ras we need to go back and update the ras so that the
574 * next read-ahead tries from where we left off. we only do so
575 * if the region we failed to issue read-ahead on is still ahead
576 * of the app and behind the next index to start read-ahead from
578 CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu\n",
579 ra_end, end, ria->ria_end);
581 if (ra_end != end + 1) {
582 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
583 spin_lock(&ras->ras_lock);
584 if (ra_end < ras->ras_next_readahead &&
585 index_in_window(ra_end, ras->ras_window_start, 0,
586 ras->ras_window_len)) {
587 ras->ras_next_readahead = ra_end;
590 spin_unlock(&ras->ras_lock);
596 static void ras_set_start(struct inode *inode, struct ll_readahead_state *ras,
599 ras->ras_window_start = index & (~(RAS_INCREASE_STEP(inode) - 1));
602 /* called with the ras_lock held or from places where it doesn't matter */
603 static void ras_reset(struct inode *inode, struct ll_readahead_state *ras,
606 ras->ras_last_readpage = index;
607 ras->ras_consecutive_requests = 0;
608 ras->ras_consecutive_pages = 0;
609 ras->ras_window_len = 0;
610 ras_set_start(inode, ras, index);
611 ras->ras_next_readahead = max(ras->ras_window_start, index);
616 /* called with the ras_lock held or from places where it doesn't matter */
617 static void ras_stride_reset(struct ll_readahead_state *ras)
619 ras->ras_consecutive_stride_requests = 0;
620 ras->ras_stride_length = 0;
621 ras->ras_stride_pages = 0;
625 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
627 spin_lock_init(&ras->ras_lock);
628 ras_reset(inode, ras, 0);
629 ras->ras_requests = 0;
633 * Check whether the read request is in the stride window.
634 * If it is in the stride window, return 1, otherwise return 0.
636 static int index_in_stride_window(struct ll_readahead_state *ras,
639 unsigned long stride_gap;
641 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
642 ras->ras_stride_pages == ras->ras_stride_length)
645 stride_gap = index - ras->ras_last_readpage - 1;
647 /* If it is contiguous read */
649 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
651 /* Otherwise check the stride by itself */
652 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
653 ras->ras_consecutive_pages == ras->ras_stride_pages;
656 static void ras_update_stride_detector(struct ll_readahead_state *ras,
659 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
661 if ((stride_gap != 0 || ras->ras_consecutive_stride_requests == 0) &&
662 !stride_io_mode(ras)) {
663 ras->ras_stride_pages = ras->ras_consecutive_pages;
664 ras->ras_stride_length = ras->ras_consecutive_pages +
667 LASSERT(ras->ras_request_index == 0);
668 LASSERT(ras->ras_consecutive_stride_requests == 0);
670 if (index <= ras->ras_last_readpage) {
671 /*Reset stride window for forward read*/
672 ras_stride_reset(ras);
676 ras->ras_stride_pages = ras->ras_consecutive_pages;
677 ras->ras_stride_length = stride_gap + ras->ras_consecutive_pages;
682 /* Stride Read-ahead window will be increased inc_len according to
685 static void ras_stride_increase_window(struct ll_readahead_state *ras,
686 struct ll_ra_info *ra,
687 unsigned long inc_len)
689 unsigned long left, step, window_len;
690 unsigned long stride_len;
692 LASSERT(ras->ras_stride_length > 0);
693 LASSERTF(ras->ras_window_start + ras->ras_window_len
694 >= ras->ras_stride_offset, "window_start %lu, window_len %lu stride_offset %lu\n",
695 ras->ras_window_start,
696 ras->ras_window_len, ras->ras_stride_offset);
698 stride_len = ras->ras_window_start + ras->ras_window_len -
699 ras->ras_stride_offset;
701 left = stride_len % ras->ras_stride_length;
702 window_len = ras->ras_window_len - left;
704 if (left < ras->ras_stride_pages)
707 left = ras->ras_stride_pages + inc_len;
709 LASSERT(ras->ras_stride_pages != 0);
711 step = left / ras->ras_stride_pages;
712 left %= ras->ras_stride_pages;
714 window_len += step * ras->ras_stride_length + left;
716 if (stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
717 ras->ras_stride_pages, ras->ras_stride_offset,
718 window_len) <= ra->ra_max_pages_per_file)
719 ras->ras_window_len = window_len;
724 static void ras_increase_window(struct inode *inode,
725 struct ll_readahead_state *ras,
726 struct ll_ra_info *ra)
728 /* The stretch of ra-window should be aligned with max rpc_size
729 * but current clio architecture does not support retrieve such
730 * information from lower layer. FIXME later
732 if (stride_io_mode(ras))
733 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP(inode));
735 ras->ras_window_len = min(ras->ras_window_len +
736 RAS_INCREASE_STEP(inode),
737 ra->ra_max_pages_per_file);
740 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
741 struct ll_readahead_state *ras, unsigned long index,
744 struct ll_ra_info *ra = &sbi->ll_ra_info;
745 int zero = 0, stride_detect = 0, ra_miss = 0;
747 spin_lock(&ras->ras_lock);
749 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
751 /* reset the read-ahead window in two cases. First when the app seeks
752 * or reads to some other part of the file. Secondly if we get a
753 * read-ahead miss that we think we've previously issued. This can
754 * be a symptom of there being so many read-ahead pages that the VM is
755 * reclaiming it before we get to it.
757 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
759 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
760 } else if (!hit && ras->ras_window_len &&
761 index < ras->ras_next_readahead &&
762 index_in_window(index, ras->ras_window_start, 0,
763 ras->ras_window_len)) {
765 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
768 /* On the second access to a file smaller than the tunable
769 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
770 * file up to ra_max_pages_per_file. This is simply a best effort
771 * and only occurs once per open file. Normal RA behavior is reverted
772 * to for subsequent IO. The mmap case does not increment
773 * ras_requests and thus can never trigger this behavior.
775 if (ras->ras_requests == 2 && !ras->ras_request_index) {
778 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
781 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
782 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
785 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
786 ras->ras_window_start = 0;
787 ras->ras_last_readpage = 0;
788 ras->ras_next_readahead = 0;
789 ras->ras_window_len = min(ra->ra_max_pages_per_file,
790 ra->ra_max_read_ahead_whole_pages);
795 /* check whether it is in stride I/O mode*/
796 if (!index_in_stride_window(ras, index)) {
797 if (ras->ras_consecutive_stride_requests == 0 &&
798 ras->ras_request_index == 0) {
799 ras_update_stride_detector(ras, index);
800 ras->ras_consecutive_stride_requests++;
802 ras_stride_reset(ras);
804 ras_reset(inode, ras, index);
805 ras->ras_consecutive_pages++;
808 ras->ras_consecutive_pages = 0;
809 ras->ras_consecutive_requests = 0;
810 if (++ras->ras_consecutive_stride_requests > 1)
816 if (index_in_stride_window(ras, index) &&
817 stride_io_mode(ras)) {
818 /*If stride-RA hit cache miss, the stride dector
819 *will not be reset to avoid the overhead of
820 *redetecting read-ahead mode
822 if (index != ras->ras_last_readpage + 1)
823 ras->ras_consecutive_pages = 0;
824 ras_reset(inode, ras, index);
827 /* Reset both stride window and normal RA
830 ras_reset(inode, ras, index);
831 ras->ras_consecutive_pages++;
832 ras_stride_reset(ras);
835 } else if (stride_io_mode(ras)) {
836 /* If this is contiguous read but in stride I/O mode
837 * currently, check whether stride step still is valid,
838 * if invalid, it will reset the stride ra window
840 if (!index_in_stride_window(ras, index)) {
841 /* Shrink stride read-ahead window to be zero */
842 ras_stride_reset(ras);
843 ras->ras_window_len = 0;
844 ras->ras_next_readahead = index;
848 ras->ras_consecutive_pages++;
849 ras->ras_last_readpage = index;
850 ras_set_start(inode, ras, index);
852 if (stride_io_mode(ras)) {
853 /* Since stride readahead is sensitive to the offset
854 * of read-ahead, so we use original offset here,
855 * instead of ras_window_start, which is RPC aligned
857 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
859 if (ras->ras_next_readahead < ras->ras_window_start)
860 ras->ras_next_readahead = ras->ras_window_start;
862 ras->ras_next_readahead = index + 1;
866 /* Trigger RA in the mmap case where ras_consecutive_requests
867 * is not incremented and thus can't be used to trigger RA
869 if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
870 ras->ras_window_len = RAS_INCREASE_STEP(inode);
874 /* Initially reset the stride window offset to next_readahead*/
875 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
877 * Once stride IO mode is detected, next_readahead should be
878 * reset to make sure next_readahead > stride offset
880 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
881 ras->ras_stride_offset = index;
882 ras->ras_window_len = RAS_INCREASE_STEP(inode);
885 /* The initial ras_window_len is set to the request size. To avoid
886 * uselessly reading and discarding pages for random IO the window is
887 * only increased once per consecutive request received. */
888 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
889 !ras->ras_request_index)
890 ras_increase_window(inode, ras, ra);
893 ras->ras_request_index++;
894 spin_unlock(&ras->ras_lock);
897 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
899 struct inode *inode = vmpage->mapping->host;
900 struct ll_inode_info *lli = ll_i2info(inode);
903 struct cl_page *page;
904 struct cl_object *clob;
905 struct cl_env_nest nest;
906 bool redirtied = false;
907 bool unlocked = false;
910 LASSERT(PageLocked(vmpage));
911 LASSERT(!PageWriteback(vmpage));
913 LASSERT(ll_i2dtexp(inode));
915 env = cl_env_nested_get(&nest);
917 result = PTR_ERR(env);
921 clob = ll_i2info(inode)->lli_clob;
924 io = vvp_env_thread_io(env);
926 io->ci_ignore_layout = 1;
927 result = cl_io_init(env, io, CIT_MISC, clob);
929 page = cl_page_find(env, clob, vmpage->index,
930 vmpage, CPT_CACHEABLE);
932 lu_ref_add(&page->cp_reference, "writepage",
934 cl_page_assume(env, io, page);
935 result = cl_page_flush(env, io, page);
938 * Re-dirty page on error so it retries write,
939 * but not in case when IO has actually
940 * occurred and completed with an error.
942 if (!PageError(vmpage)) {
943 redirty_page_for_writepage(wbc, vmpage);
948 cl_page_disown(env, io, page);
950 lu_ref_del(&page->cp_reference,
951 "writepage", current);
952 cl_page_put(env, page);
954 result = PTR_ERR(page);
959 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
960 loff_t offset = cl_offset(clob, vmpage->index);
962 /* Flush page failed because the extent is being written out.
963 * Wait for the write of extent to be finished to avoid
964 * breaking kernel which assumes ->writepage should mark
965 * PageWriteback or clean the page.
967 result = cl_sync_file_range(inode, offset,
968 offset + PAGE_SIZE - 1,
971 /* actually we may have written more than one page.
972 * decreasing this page because the caller will count
975 wbc->nr_to_write -= result - 1;
980 cl_env_nested_put(&nest, env);
985 if (!lli->lli_async_rc)
986 lli->lli_async_rc = result;
987 SetPageError(vmpage);
994 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
996 struct inode *inode = mapping->host;
997 struct ll_sb_info *sbi = ll_i2sbi(inode);
1000 enum cl_fsync_mode mode;
1001 int range_whole = 0;
1003 int ignore_layout = 0;
1005 if (wbc->range_cyclic) {
1006 start = mapping->writeback_index << PAGE_SHIFT;
1007 end = OBD_OBJECT_EOF;
1009 start = wbc->range_start;
1010 end = wbc->range_end;
1011 if (end == LLONG_MAX) {
1012 end = OBD_OBJECT_EOF;
1013 range_whole = start == 0;
1017 mode = CL_FSYNC_NONE;
1018 if (wbc->sync_mode == WB_SYNC_ALL)
1019 mode = CL_FSYNC_LOCAL;
1021 if (sbi->ll_umounting)
1022 /* if the mountpoint is being umounted, all pages have to be
1023 * evicted to avoid hitting LBUG when truncate_inode_pages()
1024 * is called later on.
1028 if (!ll_i2info(inode)->lli_clob)
1031 result = cl_sync_file_range(inode, start, end, mode, ignore_layout);
1033 wbc->nr_to_write -= result;
1037 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1038 if (end == OBD_OBJECT_EOF)
1039 mapping->writeback_index = 0;
1041 mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
1046 struct ll_cl_context *ll_cl_find(struct file *file)
1048 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1049 struct ll_cl_context *lcc;
1050 struct ll_cl_context *found = NULL;
1052 read_lock(&fd->fd_lock);
1053 list_for_each_entry(lcc, &fd->fd_lccs, lcc_list) {
1054 if (lcc->lcc_cookie == current) {
1059 read_unlock(&fd->fd_lock);
1064 void ll_cl_add(struct file *file, const struct lu_env *env, struct cl_io *io)
1066 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1067 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1069 memset(lcc, 0, sizeof(*lcc));
1070 INIT_LIST_HEAD(&lcc->lcc_list);
1071 lcc->lcc_cookie = current;
1075 write_lock(&fd->fd_lock);
1076 list_add(&lcc->lcc_list, &fd->fd_lccs);
1077 write_unlock(&fd->fd_lock);
1080 void ll_cl_remove(struct file *file, const struct lu_env *env)
1082 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1083 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1085 write_lock(&fd->fd_lock);
1086 list_del_init(&lcc->lcc_list);
1087 write_unlock(&fd->fd_lock);
1090 int ll_readpage(struct file *file, struct page *vmpage)
1092 struct cl_object *clob = ll_i2info(file_inode(file))->lli_clob;
1093 struct ll_cl_context *lcc;
1094 const struct lu_env *env;
1096 struct cl_page *page;
1099 lcc = ll_cl_find(file);
1101 unlock_page(vmpage);
1107 LASSERT(io->ci_state == CIS_IO_GOING);
1108 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
1109 if (!IS_ERR(page)) {
1110 LASSERT(page->cp_type == CPT_CACHEABLE);
1111 if (likely(!PageUptodate(vmpage))) {
1112 cl_page_assume(env, io, page);
1113 result = cl_io_read_page(env, io, page);
1115 /* Page from a non-object file. */
1116 unlock_page(vmpage);
1119 cl_page_put(env, page);
1121 unlock_page(vmpage);
1122 result = PTR_ERR(page);
1127 int ll_page_sync_io(const struct lu_env *env, struct cl_io *io,
1128 struct cl_page *page, enum cl_req_type crt)
1130 struct cl_2queue *queue;
1133 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
1135 queue = &io->ci_queue;
1136 cl_2queue_init_page(queue, page);
1138 result = cl_io_submit_sync(env, io, crt, queue, 0);
1139 LASSERT(cl_page_is_owned(page, io));
1141 if (crt == CRT_READ)
1143 * in CRT_WRITE case page is left locked even in case of
1146 cl_page_list_disown(env, io, &queue->c2_qin);
1147 cl_2queue_fini(env, queue);