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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Lustre Lite I/O page cache routines shared by different kernel revs
41 #include <linux/kernel.h>
43 #include <linux/string.h>
44 #include <linux/stat.h>
45 #include <linux/errno.h>
46 #include <linux/unistd.h>
47 #include <linux/writeback.h>
48 #include <linux/uaccess.h>
51 #include <linux/pagemap.h>
52 /* current_is_kswapd() */
53 #include <linux/swap.h>
55 #define DEBUG_SUBSYSTEM S_LLITE
57 #include "../include/lustre_lite.h"
58 #include "../include/obd_cksum.h"
59 #include "llite_internal.h"
60 #include "../include/linux/lustre_compat25.h"
62 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
65 * Get readahead pages from the filesystem readahead pool of the client for a
68 * /param sbi superblock for filesystem readahead state ll_ra_info
69 * /param ria per-thread readahead state
70 * /param pages number of pages requested for readahead for the thread.
72 * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
73 * It should work well if the ra_max_pages is much greater than the single
74 * file's read-ahead window, and not too many threads contending for
75 * these readahead pages.
77 * TODO: There may be a 'global sync problem' if many threads are trying
78 * to get an ra budget that is larger than the remaining readahead pages
79 * and reach here at exactly the same time. They will compute /a ret to
80 * consume the remaining pages, but will fail at atomic_add_return() and
81 * get a zero ra window, although there is still ra space remaining. - Jay
83 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
84 struct ra_io_arg *ria,
85 unsigned long pages, unsigned long min)
87 struct ll_ra_info *ra = &sbi->ll_ra_info;
90 /* If read-ahead pages left are less than 1M, do not do read-ahead,
91 * otherwise it will form small read RPC(< 1M), which hurt server
94 ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages), pages);
95 if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages)) {
100 /* If the non-strided (ria_pages == 0) readahead window
101 * (ria_start + ret) has grown across an RPC boundary, then trim
102 * readahead size by the amount beyond the RPC so it ends on an
103 * RPC boundary. If the readahead window is already ending on
104 * an RPC boundary (beyond_rpc == 0), or smaller than a full
105 * RPC (beyond_rpc < ret) the readahead size is unchanged.
106 * The (beyond_rpc != 0) check is skipped since the conditional
107 * branch is more expensive than subtracting zero from the result.
109 * Strided read is left unaligned to avoid small fragments beyond
110 * the RPC boundary from needing an extra read RPC.
112 if (ria->ria_pages == 0) {
113 long beyond_rpc = (ria->ria_start + ret) % PTLRPC_MAX_BRW_PAGES;
115 if (/* beyond_rpc != 0 && */ beyond_rpc < ret)
119 if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
120 atomic_sub(ret, &ra->ra_cur_pages);
126 /* override ra limit for maximum performance */
127 atomic_add(min - ret, &ra->ra_cur_pages);
133 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
135 struct ll_ra_info *ra = &sbi->ll_ra_info;
137 atomic_sub(len, &ra->ra_cur_pages);
140 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
142 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
143 lprocfs_counter_incr(sbi->ll_ra_stats, which);
146 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
148 struct ll_sb_info *sbi = ll_i2sbi(inode);
150 ll_ra_stats_inc_sbi(sbi, which);
153 #define RAS_CDEBUG(ras) \
155 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu" \
156 "csr %lu sf %lu sp %lu sl %lu\n", \
157 ras->ras_last_readpage, ras->ras_consecutive_requests, \
158 ras->ras_consecutive_pages, ras->ras_window_start, \
159 ras->ras_window_len, ras->ras_next_readahead, \
160 ras->ras_requests, ras->ras_request_index, \
161 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
162 ras->ras_stride_pages, ras->ras_stride_length)
164 static int index_in_window(unsigned long index, unsigned long point,
165 unsigned long before, unsigned long after)
167 unsigned long start = point - before, end = point + after;
174 return start <= index && index <= end;
177 void ll_ras_enter(struct file *f)
179 struct ll_file_data *fd = LUSTRE_FPRIVATE(f);
180 struct ll_readahead_state *ras = &fd->fd_ras;
182 spin_lock(&ras->ras_lock);
184 ras->ras_request_index = 0;
185 ras->ras_consecutive_requests++;
186 spin_unlock(&ras->ras_lock);
189 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
190 struct cl_page_list *queue, struct cl_page *page,
191 struct cl_object *clob, pgoff_t *max_index)
193 struct page *vmpage = page->cp_vmpage;
194 struct vvp_page *vpg;
198 cl_page_assume(env, io, page);
199 lu_ref_add(&page->cp_reference, "ra", current);
200 vpg = cl2vvp_page(cl_object_page_slice(clob, page));
201 if (!vpg->vpg_defer_uptodate && !PageUptodate(vmpage)) {
202 CDEBUG(D_READA, "page index %lu, max_index: %lu\n",
203 vvp_index(vpg), *max_index);
204 if (*max_index == 0 || vvp_index(vpg) > *max_index)
205 rc = cl_page_is_under_lock(env, io, page, max_index);
207 vpg->vpg_defer_uptodate = 1;
208 vpg->vpg_ra_used = 0;
209 cl_page_list_add(queue, page);
212 cl_page_discard(env, io, page);
216 /* skip completed pages */
217 cl_page_unassume(env, io, page);
219 lu_ref_del(&page->cp_reference, "ra", current);
220 cl_page_put(env, page);
225 * Initiates read-ahead of a page with given index.
227 * \retval +ve: page was added to \a queue.
229 * \retval -ENOLCK: there is no extent lock for this part of a file, stop
232 * \retval -ve, 0: page wasn't added to \a queue for other reason.
234 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
235 struct cl_page_list *queue,
236 pgoff_t index, pgoff_t *max_index)
238 struct cl_object *clob = io->ci_obj;
239 struct inode *inode = vvp_object_inode(clob);
241 struct cl_page *page;
242 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
244 const char *msg = NULL;
246 vmpage = grab_cache_page_nowait(inode->i_mapping, index);
248 /* Check if vmpage was truncated or reclaimed */
249 if (vmpage->mapping == inode->i_mapping) {
250 page = cl_page_find(env, clob, vmpage->index,
251 vmpage, CPT_CACHEABLE);
253 rc = cl_read_ahead_page(env, io, queue,
254 page, clob, max_index);
256 which = RA_STAT_FAILED_MATCH;
257 msg = "lock match failed";
260 which = RA_STAT_FAILED_GRAB_PAGE;
261 msg = "cl_page_find failed";
264 which = RA_STAT_WRONG_GRAB_PAGE;
265 msg = "g_c_p_n returned invalid page";
271 which = RA_STAT_FAILED_GRAB_PAGE;
272 msg = "g_c_p_n failed";
275 ll_ra_stats_inc(inode, which);
276 CDEBUG(D_READA, "%s\n", msg);
281 #define RIA_DEBUG(ria) \
282 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
283 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
286 /* Limit this to the blocksize instead of PTLRPC_BRW_MAX_SIZE, since we don't
287 * know what the actual RPC size is. If this needs to change, it makes more
288 * sense to tune the i_blkbits value for the file based on the OSTs it is
289 * striped over, rather than having a constant value for all files here.
292 /* RAS_INCREASE_STEP should be (1UL << (inode->i_blkbits - PAGE_SHIFT)).
293 * Temporarily set RAS_INCREASE_STEP to 1MB. After 4MB RPC is enabled
294 * by default, this should be adjusted corresponding with max_read_ahead_mb
295 * and max_read_ahead_per_file_mb otherwise the readahead budget can be used
296 * up quickly which will affect read performance significantly. See LU-2816
298 #define RAS_INCREASE_STEP(inode) (ONE_MB_BRW_SIZE >> PAGE_SHIFT)
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 much pages will be read in
306 * [off, off + length], in such stride IO area,
307 * stride_offset = st_off, stride_length = st_len,
308 * stride_pages = st_pgs
310 * |------------------|*****|------------------|*****|------------|*****|....
313 * |----- st_len -----|
315 * How many pages it should read in such pattern
316 * |-------------------------------------------------------------|
318 * |<------ length ------->|
320 * = |<----->| + |-------------------------------------| + |---|
321 * start_left st_pgs * i end_left
324 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
325 unsigned long off, unsigned long length)
327 __u64 start = off > st_off ? off - st_off : 0;
328 __u64 end = off + length > st_off ? off + length - st_off : 0;
329 unsigned long start_left = 0;
330 unsigned long end_left = 0;
331 unsigned long pg_count;
333 if (st_len == 0 || length == 0 || end == 0)
336 start_left = do_div(start, st_len);
337 if (start_left < st_pgs)
338 start_left = st_pgs - start_left;
342 end_left = do_div(end, st_len);
343 if (end_left > st_pgs)
346 CDEBUG(D_READA, "start %llu, end %llu start_left %lu end_left %lu\n",
347 start, end, start_left, end_left);
350 pg_count = end_left - (st_pgs - start_left);
352 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
354 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu pgcount %lu\n",
355 st_off, st_len, st_pgs, off, length, pg_count);
360 static int ria_page_count(struct ra_io_arg *ria)
362 __u64 length = ria->ria_end >= ria->ria_start ?
363 ria->ria_end - ria->ria_start + 1 : 0;
365 return stride_pg_count(ria->ria_stoff, ria->ria_length,
366 ria->ria_pages, ria->ria_start,
370 /*Check whether the index is in the defined ra-window */
371 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
373 /* If ria_length == ria_pages, it means non-stride I/O mode,
374 * idx should always inside read-ahead window in this case
375 * For stride I/O mode, just check whether the idx is inside
378 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
379 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
380 ria->ria_length < ria->ria_pages);
383 static int ll_read_ahead_pages(const struct lu_env *env,
384 struct cl_io *io, struct cl_page_list *queue,
385 struct ra_io_arg *ria,
386 unsigned long *reserved_pages,
387 unsigned long *ra_end)
392 pgoff_t max_index = 0;
397 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
398 for (page_idx = ria->ria_start;
399 page_idx <= ria->ria_end && *reserved_pages > 0; page_idx++) {
400 if (ras_inside_ra_window(page_idx, ria)) {
401 /* If the page is inside the read-ahead window*/
402 rc = ll_read_ahead_page(env, io, queue,
403 page_idx, &max_index);
407 } else if (rc == -ENOLCK) {
410 } else if (stride_ria) {
411 /* If it is not in the read-ahead window, and it is
412 * read-ahead mode, then check whether it should skip
416 /* FIXME: This assertion only is valid when it is for
417 * forward read-ahead, it will be fixed when backward
418 * read-ahead is implemented
420 LASSERTF(page_idx > ria->ria_stoff, "Invalid page_idx %lu rs %lu re %lu ro %lu rl %lu rp %lu\n",
422 ria->ria_start, ria->ria_end, ria->ria_stoff,
423 ria->ria_length, ria->ria_pages);
424 offset = page_idx - ria->ria_stoff;
425 offset = offset % (ria->ria_length);
426 if (offset > ria->ria_pages) {
427 page_idx += ria->ria_length - offset;
428 CDEBUG(D_READA, "i %lu skip %lu\n", page_idx,
429 ria->ria_length - offset);
438 int ll_readahead(const struct lu_env *env, struct cl_io *io,
439 struct cl_page_list *queue, struct ll_readahead_state *ras,
442 struct vvp_io *vio = vvp_env_io(env);
443 struct ll_thread_info *lti = ll_env_info(env);
444 struct cl_attr *attr = vvp_env_thread_attr(env);
445 unsigned long start = 0, end = 0, reserved;
446 unsigned long ra_end, len, mlen = 0;
448 struct ra_io_arg *ria = <i->lti_ria;
449 struct cl_object *clob;
454 inode = vvp_object_inode(clob);
456 memset(ria, 0, sizeof(*ria));
458 cl_object_attr_lock(clob);
459 ret = cl_object_attr_get(env, clob, attr);
460 cl_object_attr_unlock(clob);
466 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
470 spin_lock(&ras->ras_lock);
472 /* Enlarge the RA window to encompass the full read */
473 if (vio->vui_ra_valid &&
474 ras->ras_window_start + ras->ras_window_len <
475 vio->vui_ra_start + vio->vui_ra_count) {
476 ras->ras_window_len = vio->vui_ra_start + vio->vui_ra_count -
477 ras->ras_window_start;
480 /* Reserve a part of the read-ahead window that we'll be issuing */
481 if (ras->ras_window_len) {
482 start = ras->ras_next_readahead;
483 end = ras->ras_window_start + ras->ras_window_len - 1;
486 unsigned long rpc_boundary;
488 * Align RA window to an optimal boundary.
490 * XXX This would be better to align to cl_max_pages_per_rpc
491 * instead of PTLRPC_MAX_BRW_PAGES, because the RPC size may
492 * be aligned to the RAID stripe size in the future and that
493 * is more important than the RPC size.
495 /* Note: we only trim the RPC, instead of extending the RPC
496 * to the boundary, so to avoid reading too much pages during
499 rpc_boundary = (end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1));
500 if (rpc_boundary > 0)
503 if (rpc_boundary > start)
506 /* Truncate RA window to end of file */
507 end = min(end, (unsigned long)((kms - 1) >> PAGE_SHIFT));
509 ras->ras_next_readahead = max(end, end + 1);
512 ria->ria_start = start;
514 /* If stride I/O mode is detected, get stride window*/
515 if (stride_io_mode(ras)) {
516 ria->ria_stoff = ras->ras_stride_offset;
517 ria->ria_length = ras->ras_stride_length;
518 ria->ria_pages = ras->ras_stride_pages;
520 spin_unlock(&ras->ras_lock);
523 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
526 len = ria_page_count(ria);
528 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
532 CDEBUG(D_READA, DFID ": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
533 PFID(lu_object_fid(&clob->co_lu)),
534 ria->ria_start, ria->ria_end,
535 vio->vui_ra_valid ? vio->vui_ra_start : 0,
536 vio->vui_ra_valid ? vio->vui_ra_count : 0,
539 /* at least to extend the readahead window to cover current read */
540 if (!hit && vio->vui_ra_valid &&
541 vio->vui_ra_start + vio->vui_ra_count > ria->ria_start) {
542 /* to the end of current read window. */
543 mlen = vio->vui_ra_start + vio->vui_ra_count - ria->ria_start;
544 /* trim to RPC boundary */
545 start = ria->ria_start & (PTLRPC_MAX_BRW_PAGES - 1);
546 mlen = min(mlen, PTLRPC_MAX_BRW_PAGES - start);
549 reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len, mlen);
551 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
553 CDEBUG(D_READA, "reserved pages %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
555 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
556 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
558 ret = ll_read_ahead_pages(env, io, queue, ria, &reserved, &ra_end);
561 ll_ra_count_put(ll_i2sbi(inode), reserved);
563 if (ra_end == end + 1 && ra_end == (kms >> PAGE_SHIFT))
564 ll_ra_stats_inc(inode, RA_STAT_EOF);
566 /* if we didn't get to the end of the region we reserved from
567 * the ras we need to go back and update the ras so that the
568 * next read-ahead tries from where we left off. we only do so
569 * if the region we failed to issue read-ahead on is still ahead
570 * of the app and behind the next index to start read-ahead from
572 CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu\n",
573 ra_end, end, ria->ria_end);
575 if (ra_end != end + 1) {
576 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
577 spin_lock(&ras->ras_lock);
578 if (ra_end < ras->ras_next_readahead &&
579 index_in_window(ra_end, ras->ras_window_start, 0,
580 ras->ras_window_len)) {
581 ras->ras_next_readahead = ra_end;
584 spin_unlock(&ras->ras_lock);
590 static void ras_set_start(struct inode *inode, struct ll_readahead_state *ras,
593 ras->ras_window_start = index & (~(RAS_INCREASE_STEP(inode) - 1));
596 /* called with the ras_lock held or from places where it doesn't matter */
597 static void ras_reset(struct inode *inode, struct ll_readahead_state *ras,
600 ras->ras_last_readpage = index;
601 ras->ras_consecutive_requests = 0;
602 ras->ras_consecutive_pages = 0;
603 ras->ras_window_len = 0;
604 ras_set_start(inode, ras, index);
605 ras->ras_next_readahead = max(ras->ras_window_start, index);
610 /* called with the ras_lock held or from places where it doesn't matter */
611 static void ras_stride_reset(struct ll_readahead_state *ras)
613 ras->ras_consecutive_stride_requests = 0;
614 ras->ras_stride_length = 0;
615 ras->ras_stride_pages = 0;
619 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
621 spin_lock_init(&ras->ras_lock);
622 ras_reset(inode, ras, 0);
623 ras->ras_requests = 0;
627 * Check whether the read request is in the stride window.
628 * If it is in the stride window, return 1, otherwise return 0.
630 static int index_in_stride_window(struct ll_readahead_state *ras,
633 unsigned long stride_gap;
635 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
636 ras->ras_stride_pages == ras->ras_stride_length)
639 stride_gap = index - ras->ras_last_readpage - 1;
641 /* If it is contiguous read */
643 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
645 /* Otherwise check the stride by itself */
646 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
647 ras->ras_consecutive_pages == ras->ras_stride_pages;
650 static void ras_update_stride_detector(struct ll_readahead_state *ras,
653 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
655 if (!stride_io_mode(ras) && (stride_gap != 0 ||
656 ras->ras_consecutive_stride_requests == 0)) {
657 ras->ras_stride_pages = ras->ras_consecutive_pages;
658 ras->ras_stride_length = stride_gap+ras->ras_consecutive_pages;
660 LASSERT(ras->ras_request_index == 0);
661 LASSERT(ras->ras_consecutive_stride_requests == 0);
663 if (index <= ras->ras_last_readpage) {
664 /*Reset stride window for forward read*/
665 ras_stride_reset(ras);
669 ras->ras_stride_pages = ras->ras_consecutive_pages;
670 ras->ras_stride_length = stride_gap+ras->ras_consecutive_pages;
676 /* Stride Read-ahead window will be increased inc_len according to
679 static void ras_stride_increase_window(struct ll_readahead_state *ras,
680 struct ll_ra_info *ra,
681 unsigned long inc_len)
683 unsigned long left, step, window_len;
684 unsigned long stride_len;
686 LASSERT(ras->ras_stride_length > 0);
687 LASSERTF(ras->ras_window_start + ras->ras_window_len
688 >= ras->ras_stride_offset, "window_start %lu, window_len %lu stride_offset %lu\n",
689 ras->ras_window_start,
690 ras->ras_window_len, ras->ras_stride_offset);
692 stride_len = ras->ras_window_start + ras->ras_window_len -
693 ras->ras_stride_offset;
695 left = stride_len % ras->ras_stride_length;
696 window_len = ras->ras_window_len - left;
698 if (left < ras->ras_stride_pages)
701 left = ras->ras_stride_pages + inc_len;
703 LASSERT(ras->ras_stride_pages != 0);
705 step = left / ras->ras_stride_pages;
706 left %= ras->ras_stride_pages;
708 window_len += step * ras->ras_stride_length + left;
710 if (stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
711 ras->ras_stride_pages, ras->ras_stride_offset,
712 window_len) <= ra->ra_max_pages_per_file)
713 ras->ras_window_len = window_len;
718 static void ras_increase_window(struct inode *inode,
719 struct ll_readahead_state *ras,
720 struct ll_ra_info *ra)
722 /* The stretch of ra-window should be aligned with max rpc_size
723 * but current clio architecture does not support retrieve such
724 * information from lower layer. FIXME later
726 if (stride_io_mode(ras))
727 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP(inode));
729 ras->ras_window_len = min(ras->ras_window_len +
730 RAS_INCREASE_STEP(inode),
731 ra->ra_max_pages_per_file);
734 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
735 struct ll_readahead_state *ras, unsigned long index,
738 struct ll_ra_info *ra = &sbi->ll_ra_info;
739 int zero = 0, stride_detect = 0, ra_miss = 0;
741 spin_lock(&ras->ras_lock);
743 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
745 /* reset the read-ahead window in two cases. First when the app seeks
746 * or reads to some other part of the file. Secondly if we get a
747 * read-ahead miss that we think we've previously issued. This can
748 * be a symptom of there being so many read-ahead pages that the VM is
749 * reclaiming it before we get to it.
751 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
753 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
754 } else if (!hit && ras->ras_window_len &&
755 index < ras->ras_next_readahead &&
756 index_in_window(index, ras->ras_window_start, 0,
757 ras->ras_window_len)) {
759 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
762 /* On the second access to a file smaller than the tunable
763 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
764 * file up to ra_max_pages_per_file. This is simply a best effort
765 * and only occurs once per open file. Normal RA behavior is reverted
766 * to for subsequent IO. The mmap case does not increment
767 * ras_requests and thus can never trigger this behavior.
769 if (ras->ras_requests == 2 && !ras->ras_request_index) {
772 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
775 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
776 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
779 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
780 ras->ras_window_start = 0;
781 ras->ras_last_readpage = 0;
782 ras->ras_next_readahead = 0;
783 ras->ras_window_len = min(ra->ra_max_pages_per_file,
784 ra->ra_max_read_ahead_whole_pages);
789 /* check whether it is in stride I/O mode*/
790 if (!index_in_stride_window(ras, index)) {
791 if (ras->ras_consecutive_stride_requests == 0 &&
792 ras->ras_request_index == 0) {
793 ras_update_stride_detector(ras, index);
794 ras->ras_consecutive_stride_requests++;
796 ras_stride_reset(ras);
798 ras_reset(inode, ras, index);
799 ras->ras_consecutive_pages++;
802 ras->ras_consecutive_pages = 0;
803 ras->ras_consecutive_requests = 0;
804 if (++ras->ras_consecutive_stride_requests > 1)
810 if (index_in_stride_window(ras, index) &&
811 stride_io_mode(ras)) {
812 /*If stride-RA hit cache miss, the stride dector
813 *will not be reset to avoid the overhead of
814 *redetecting read-ahead mode
816 if (index != ras->ras_last_readpage + 1)
817 ras->ras_consecutive_pages = 0;
818 ras_reset(inode, ras, index);
821 /* Reset both stride window and normal RA
824 ras_reset(inode, ras, index);
825 ras->ras_consecutive_pages++;
826 ras_stride_reset(ras);
829 } else if (stride_io_mode(ras)) {
830 /* If this is contiguous read but in stride I/O mode
831 * currently, check whether stride step still is valid,
832 * if invalid, it will reset the stride ra window
834 if (!index_in_stride_window(ras, index)) {
835 /* Shrink stride read-ahead window to be zero */
836 ras_stride_reset(ras);
837 ras->ras_window_len = 0;
838 ras->ras_next_readahead = index;
842 ras->ras_consecutive_pages++;
843 ras->ras_last_readpage = index;
844 ras_set_start(inode, ras, index);
846 if (stride_io_mode(ras)) {
847 /* Since stride readahead is sensitive to the offset
848 * of read-ahead, so we use original offset here,
849 * instead of ras_window_start, which is RPC aligned
851 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
853 if (ras->ras_next_readahead < ras->ras_window_start)
854 ras->ras_next_readahead = ras->ras_window_start;
856 ras->ras_next_readahead = index + 1;
860 /* Trigger RA in the mmap case where ras_consecutive_requests
861 * is not incremented and thus can't be used to trigger RA
863 if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
864 ras->ras_window_len = RAS_INCREASE_STEP(inode);
868 /* Initially reset the stride window offset to next_readahead*/
869 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
871 * Once stride IO mode is detected, next_readahead should be
872 * reset to make sure next_readahead > stride offset
874 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
875 ras->ras_stride_offset = index;
876 ras->ras_window_len = RAS_INCREASE_STEP(inode);
879 /* The initial ras_window_len is set to the request size. To avoid
880 * uselessly reading and discarding pages for random IO the window is
881 * only increased once per consecutive request received. */
882 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
883 !ras->ras_request_index)
884 ras_increase_window(inode, ras, ra);
887 ras->ras_request_index++;
888 spin_unlock(&ras->ras_lock);
892 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
894 struct inode *inode = vmpage->mapping->host;
895 struct ll_inode_info *lli = ll_i2info(inode);
898 struct cl_page *page;
899 struct cl_object *clob;
900 struct cl_env_nest nest;
901 bool redirtied = false;
902 bool unlocked = false;
905 LASSERT(PageLocked(vmpage));
906 LASSERT(!PageWriteback(vmpage));
908 LASSERT(ll_i2dtexp(inode));
910 env = cl_env_nested_get(&nest);
912 result = PTR_ERR(env);
916 clob = ll_i2info(inode)->lli_clob;
919 io = vvp_env_thread_io(env);
921 io->ci_ignore_layout = 1;
922 result = cl_io_init(env, io, CIT_MISC, clob);
924 page = cl_page_find(env, clob, vmpage->index,
925 vmpage, CPT_CACHEABLE);
927 lu_ref_add(&page->cp_reference, "writepage",
929 cl_page_assume(env, io, page);
930 result = cl_page_flush(env, io, page);
933 * Re-dirty page on error so it retries write,
934 * but not in case when IO has actually
935 * occurred and completed with an error.
937 if (!PageError(vmpage)) {
938 redirty_page_for_writepage(wbc, vmpage);
943 cl_page_disown(env, io, page);
945 lu_ref_del(&page->cp_reference,
946 "writepage", current);
947 cl_page_put(env, page);
949 result = PTR_ERR(page);
954 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
955 loff_t offset = cl_offset(clob, vmpage->index);
957 /* Flush page failed because the extent is being written out.
958 * Wait for the write of extent to be finished to avoid
959 * breaking kernel which assumes ->writepage should mark
960 * PageWriteback or clean the page.
962 result = cl_sync_file_range(inode, offset,
963 offset + PAGE_SIZE - 1,
966 /* actually we may have written more than one page.
967 * decreasing this page because the caller will count
970 wbc->nr_to_write -= result - 1;
975 cl_env_nested_put(&nest, env);
980 if (!lli->lli_async_rc)
981 lli->lli_async_rc = result;
982 SetPageError(vmpage);
989 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
991 struct inode *inode = mapping->host;
992 struct ll_sb_info *sbi = ll_i2sbi(inode);
995 enum cl_fsync_mode mode;
998 int ignore_layout = 0;
1000 if (wbc->range_cyclic) {
1001 start = mapping->writeback_index << PAGE_SHIFT;
1002 end = OBD_OBJECT_EOF;
1004 start = wbc->range_start;
1005 end = wbc->range_end;
1006 if (end == LLONG_MAX) {
1007 end = OBD_OBJECT_EOF;
1008 range_whole = start == 0;
1012 mode = CL_FSYNC_NONE;
1013 if (wbc->sync_mode == WB_SYNC_ALL)
1014 mode = CL_FSYNC_LOCAL;
1016 if (sbi->ll_umounting)
1017 /* if the mountpoint is being umounted, all pages have to be
1018 * evicted to avoid hitting LBUG when truncate_inode_pages()
1019 * is called later on.
1022 result = cl_sync_file_range(inode, start, end, mode, ignore_layout);
1024 wbc->nr_to_write -= result;
1028 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1029 if (end == OBD_OBJECT_EOF)
1030 mapping->writeback_index = 0;
1032 mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
1037 struct ll_cl_context *ll_cl_find(struct file *file)
1039 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1040 struct ll_cl_context *lcc;
1041 struct ll_cl_context *found = NULL;
1043 read_lock(&fd->fd_lock);
1044 list_for_each_entry(lcc, &fd->fd_lccs, lcc_list) {
1045 if (lcc->lcc_cookie == current) {
1050 read_unlock(&fd->fd_lock);
1055 void ll_cl_add(struct file *file, const struct lu_env *env, struct cl_io *io)
1057 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1058 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1060 memset(lcc, 0, sizeof(*lcc));
1061 INIT_LIST_HEAD(&lcc->lcc_list);
1062 lcc->lcc_cookie = current;
1066 write_lock(&fd->fd_lock);
1067 list_add(&lcc->lcc_list, &fd->fd_lccs);
1068 write_unlock(&fd->fd_lock);
1071 void ll_cl_remove(struct file *file, const struct lu_env *env)
1073 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1074 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1076 write_lock(&fd->fd_lock);
1077 list_del_init(&lcc->lcc_list);
1078 write_unlock(&fd->fd_lock);
1081 int ll_readpage(struct file *file, struct page *vmpage)
1083 struct cl_object *clob = ll_i2info(file_inode(file))->lli_clob;
1084 struct ll_cl_context *lcc;
1085 const struct lu_env *env;
1087 struct cl_page *page;
1090 lcc = ll_cl_find(file);
1092 unlock_page(vmpage);
1098 LASSERT(io->ci_state == CIS_IO_GOING);
1099 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
1100 if (!IS_ERR(page)) {
1101 LASSERT(page->cp_type == CPT_CACHEABLE);
1102 if (likely(!PageUptodate(vmpage))) {
1103 cl_page_assume(env, io, page);
1104 result = cl_io_read_page(env, io, page);
1106 /* Page from a non-object file. */
1107 unlock_page(vmpage);
1110 cl_page_put(env, page);
1112 unlock_page(vmpage);
1113 result = PTR_ERR(page);
1118 int ll_page_sync_io(const struct lu_env *env, struct cl_io *io,
1119 struct cl_page *page, enum cl_req_type crt)
1121 struct cl_2queue *queue;
1124 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
1126 queue = &io->ci_queue;
1127 cl_2queue_init_page(queue, page);
1129 result = cl_io_submit_sync(env, io, crt, queue, 0);
1130 LASSERT(cl_page_is_owned(page, io));
1132 if (crt == CRT_READ)
1134 * in CRT_WRITE case page is left locked even in case of
1137 cl_page_list_disown(env, io, &queue->c2_qin);
1138 cl_2queue_fini(env, queue);