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/lustre_lite.h"
54 #include "../include/obd_cksum.h"
55 #include "llite_internal.h"
56 #include "../include/linux/lustre_compat25.h"
58 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
61 * Get readahead pages from the filesystem readahead pool of the client for a
64 * /param sbi superblock for filesystem readahead state ll_ra_info
65 * /param ria per-thread readahead state
66 * /param pages number of pages requested for readahead for the thread.
68 * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
69 * It should work well if the ra_max_pages is much greater than the single
70 * file's read-ahead window, and not too many threads contending for
71 * these readahead pages.
73 * TODO: There may be a 'global sync problem' if many threads are trying
74 * to get an ra budget that is larger than the remaining readahead pages
75 * and reach here at exactly the same time. They will compute /a ret to
76 * consume the remaining pages, but will fail at atomic_add_return() and
77 * get a zero ra window, although there is still ra space remaining. - Jay
79 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
80 struct ra_io_arg *ria,
81 unsigned long pages, unsigned long min)
83 struct ll_ra_info *ra = &sbi->ll_ra_info;
86 /* If read-ahead pages left are less than 1M, do not do read-ahead,
87 * otherwise it will form small read RPC(< 1M), which hurt server
90 ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages), pages);
91 if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages)) {
96 /* If the non-strided (ria_pages == 0) readahead window
97 * (ria_start + ret) has grown across an RPC boundary, then trim
98 * readahead size by the amount beyond the RPC so it ends on an
99 * RPC boundary. If the readahead window is already ending on
100 * an RPC boundary (beyond_rpc == 0), or smaller than a full
101 * RPC (beyond_rpc < ret) the readahead size is unchanged.
102 * The (beyond_rpc != 0) check is skipped since the conditional
103 * branch is more expensive than subtracting zero from the result.
105 * Strided read is left unaligned to avoid small fragments beyond
106 * the RPC boundary from needing an extra read RPC.
108 if (ria->ria_pages == 0) {
109 long beyond_rpc = (ria->ria_start + ret) % PTLRPC_MAX_BRW_PAGES;
111 if (/* beyond_rpc != 0 && */ beyond_rpc < ret)
115 if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
116 atomic_sub(ret, &ra->ra_cur_pages);
122 /* override ra limit for maximum performance */
123 atomic_add(min - ret, &ra->ra_cur_pages);
129 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
131 struct ll_ra_info *ra = &sbi->ll_ra_info;
133 atomic_sub(len, &ra->ra_cur_pages);
136 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
138 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
139 lprocfs_counter_incr(sbi->ll_ra_stats, which);
142 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
144 struct ll_sb_info *sbi = ll_i2sbi(inode);
146 ll_ra_stats_inc_sbi(sbi, which);
149 #define RAS_CDEBUG(ras) \
151 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu" \
152 "csr %lu sf %lu sp %lu sl %lu\n", \
153 ras->ras_last_readpage, ras->ras_consecutive_requests, \
154 ras->ras_consecutive_pages, ras->ras_window_start, \
155 ras->ras_window_len, ras->ras_next_readahead, \
156 ras->ras_requests, ras->ras_request_index, \
157 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
158 ras->ras_stride_pages, ras->ras_stride_length)
160 static int index_in_window(unsigned long index, unsigned long point,
161 unsigned long before, unsigned long after)
163 unsigned long start = point - before, end = point + after;
170 return start <= index && index <= end;
173 void ll_ras_enter(struct file *f)
175 struct ll_file_data *fd = LUSTRE_FPRIVATE(f);
176 struct ll_readahead_state *ras = &fd->fd_ras;
178 spin_lock(&ras->ras_lock);
180 ras->ras_request_index = 0;
181 ras->ras_consecutive_requests++;
182 spin_unlock(&ras->ras_lock);
185 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
186 struct cl_page_list *queue, struct cl_page *page,
187 struct cl_object *clob, pgoff_t *max_index)
189 struct page *vmpage = page->cp_vmpage;
190 struct vvp_page *vpg;
194 cl_page_assume(env, io, page);
195 lu_ref_add(&page->cp_reference, "ra", current);
196 vpg = cl2vvp_page(cl_object_page_slice(clob, page));
197 if (!vpg->vpg_defer_uptodate && !PageUptodate(vmpage)) {
198 CDEBUG(D_READA, "page index %lu, max_index: %lu\n",
199 vvp_index(vpg), *max_index);
200 if (*max_index == 0 || vvp_index(vpg) > *max_index)
201 rc = cl_page_is_under_lock(env, io, page, max_index);
203 vpg->vpg_defer_uptodate = 1;
204 vpg->vpg_ra_used = 0;
205 cl_page_list_add(queue, page);
208 cl_page_discard(env, io, page);
212 /* skip completed pages */
213 cl_page_unassume(env, io, page);
215 lu_ref_del(&page->cp_reference, "ra", current);
216 cl_page_put(env, page);
221 * Initiates read-ahead of a page with given index.
223 * \retval +ve: page was added to \a queue.
225 * \retval -ENOLCK: there is no extent lock for this part of a file, stop
228 * \retval -ve, 0: page wasn't added to \a queue for other reason.
230 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
231 struct cl_page_list *queue,
232 pgoff_t index, pgoff_t *max_index)
234 struct cl_object *clob = io->ci_obj;
235 struct inode *inode = vvp_object_inode(clob);
237 struct cl_page *page;
238 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
240 const char *msg = NULL;
242 vmpage = grab_cache_page_nowait(inode->i_mapping, index);
244 /* Check if vmpage was truncated or reclaimed */
245 if (vmpage->mapping == inode->i_mapping) {
246 page = cl_page_find(env, clob, vmpage->index,
247 vmpage, CPT_CACHEABLE);
249 rc = cl_read_ahead_page(env, io, queue,
250 page, clob, max_index);
252 which = RA_STAT_FAILED_MATCH;
253 msg = "lock match failed";
256 which = RA_STAT_FAILED_GRAB_PAGE;
257 msg = "cl_page_find failed";
260 which = RA_STAT_WRONG_GRAB_PAGE;
261 msg = "g_c_p_n returned invalid page";
267 which = RA_STAT_FAILED_GRAB_PAGE;
268 msg = "g_c_p_n failed";
271 ll_ra_stats_inc(inode, which);
272 CDEBUG(D_READA, "%s\n", msg);
277 #define RIA_DEBUG(ria) \
278 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
279 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
282 /* Limit this to the blocksize instead of PTLRPC_BRW_MAX_SIZE, since we don't
283 * know what the actual RPC size is. If this needs to change, it makes more
284 * sense to tune the i_blkbits value for the file based on the OSTs it is
285 * striped over, rather than having a constant value for all files here.
288 /* RAS_INCREASE_STEP should be (1UL << (inode->i_blkbits - PAGE_SHIFT)).
289 * Temporarily set RAS_INCREASE_STEP to 1MB. After 4MB RPC is enabled
290 * by default, this should be adjusted corresponding with max_read_ahead_mb
291 * and max_read_ahead_per_file_mb otherwise the readahead budget can be used
292 * up quickly which will affect read performance significantly. See LU-2816
294 #define RAS_INCREASE_STEP(inode) (ONE_MB_BRW_SIZE >> PAGE_SHIFT)
296 static inline int stride_io_mode(struct ll_readahead_state *ras)
298 return ras->ras_consecutive_stride_requests > 1;
301 /* The function calculates how much pages will be read in
302 * [off, off + length], in such stride IO area,
303 * stride_offset = st_off, stride_length = st_len,
304 * stride_pages = st_pgs
306 * |------------------|*****|------------------|*****|------------|*****|....
309 * |----- st_len -----|
311 * How many pages it should read in such pattern
312 * |-------------------------------------------------------------|
314 * |<------ length ------->|
316 * = |<----->| + |-------------------------------------| + |---|
317 * start_left st_pgs * i end_left
320 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
321 unsigned long off, unsigned long length)
323 __u64 start = off > st_off ? off - st_off : 0;
324 __u64 end = off + length > st_off ? off + length - st_off : 0;
325 unsigned long start_left = 0;
326 unsigned long end_left = 0;
327 unsigned long pg_count;
329 if (st_len == 0 || length == 0 || end == 0)
332 start_left = do_div(start, st_len);
333 if (start_left < st_pgs)
334 start_left = st_pgs - start_left;
338 end_left = do_div(end, st_len);
339 if (end_left > st_pgs)
342 CDEBUG(D_READA, "start %llu, end %llu start_left %lu end_left %lu\n",
343 start, end, start_left, end_left);
346 pg_count = end_left - (st_pgs - start_left);
348 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
350 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu pgcount %lu\n",
351 st_off, st_len, st_pgs, off, length, pg_count);
356 static int ria_page_count(struct ra_io_arg *ria)
358 __u64 length = ria->ria_end >= ria->ria_start ?
359 ria->ria_end - ria->ria_start + 1 : 0;
361 return stride_pg_count(ria->ria_stoff, ria->ria_length,
362 ria->ria_pages, ria->ria_start,
366 /*Check whether the index is in the defined ra-window */
367 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
369 /* If ria_length == ria_pages, it means non-stride I/O mode,
370 * idx should always inside read-ahead window in this case
371 * For stride I/O mode, just check whether the idx is inside
374 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
375 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
376 ria->ria_length < ria->ria_pages);
379 static int ll_read_ahead_pages(const struct lu_env *env,
380 struct cl_io *io, struct cl_page_list *queue,
381 struct ra_io_arg *ria,
382 unsigned long *reserved_pages,
383 unsigned long *ra_end)
388 pgoff_t max_index = 0;
393 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
394 for (page_idx = ria->ria_start;
395 page_idx <= ria->ria_end && *reserved_pages > 0; page_idx++) {
396 if (ras_inside_ra_window(page_idx, ria)) {
397 /* If the page is inside the read-ahead window*/
398 rc = ll_read_ahead_page(env, io, queue,
399 page_idx, &max_index);
403 } else if (rc == -ENOLCK) {
406 } else if (stride_ria) {
407 /* If it is not in the read-ahead window, and it is
408 * read-ahead mode, then check whether it should skip
412 /* FIXME: This assertion only is valid when it is for
413 * forward read-ahead, it will be fixed when backward
414 * read-ahead is implemented
416 LASSERTF(page_idx > ria->ria_stoff, "Invalid page_idx %lu rs %lu re %lu ro %lu rl %lu rp %lu\n",
418 ria->ria_start, ria->ria_end, ria->ria_stoff,
419 ria->ria_length, ria->ria_pages);
420 offset = page_idx - ria->ria_stoff;
421 offset = offset % (ria->ria_length);
422 if (offset > ria->ria_pages) {
423 page_idx += ria->ria_length - offset;
424 CDEBUG(D_READA, "i %lu skip %lu\n", page_idx,
425 ria->ria_length - offset);
434 int ll_readahead(const struct lu_env *env, struct cl_io *io,
435 struct cl_page_list *queue, struct ll_readahead_state *ras,
438 struct vvp_io *vio = vvp_env_io(env);
439 struct ll_thread_info *lti = ll_env_info(env);
440 struct cl_attr *attr = vvp_env_thread_attr(env);
441 unsigned long start = 0, end = 0, reserved;
442 unsigned long ra_end, len, mlen = 0;
444 struct ra_io_arg *ria = <i->lti_ria;
445 struct cl_object *clob;
450 inode = vvp_object_inode(clob);
452 memset(ria, 0, sizeof(*ria));
454 cl_object_attr_lock(clob);
455 ret = cl_object_attr_get(env, clob, attr);
456 cl_object_attr_unlock(clob);
462 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
466 spin_lock(&ras->ras_lock);
468 /* Enlarge the RA window to encompass the full read */
469 if (vio->vui_ra_valid &&
470 ras->ras_window_start + ras->ras_window_len <
471 vio->vui_ra_start + vio->vui_ra_count) {
472 ras->ras_window_len = vio->vui_ra_start + vio->vui_ra_count -
473 ras->ras_window_start;
476 /* Reserve a part of the read-ahead window that we'll be issuing */
477 if (ras->ras_window_len) {
478 start = ras->ras_next_readahead;
479 end = ras->ras_window_start + ras->ras_window_len - 1;
482 unsigned long rpc_boundary;
484 * Align RA window to an optimal boundary.
486 * XXX This would be better to align to cl_max_pages_per_rpc
487 * instead of PTLRPC_MAX_BRW_PAGES, because the RPC size may
488 * be aligned to the RAID stripe size in the future and that
489 * is more important than the RPC size.
491 /* Note: we only trim the RPC, instead of extending the RPC
492 * to the boundary, so to avoid reading too much pages during
495 rpc_boundary = (end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1));
496 if (rpc_boundary > 0)
499 if (rpc_boundary > start)
502 /* Truncate RA window to end of file */
503 end = min(end, (unsigned long)((kms - 1) >> PAGE_SHIFT));
505 ras->ras_next_readahead = max(end, end + 1);
508 ria->ria_start = start;
510 /* If stride I/O mode is detected, get stride window*/
511 if (stride_io_mode(ras)) {
512 ria->ria_stoff = ras->ras_stride_offset;
513 ria->ria_length = ras->ras_stride_length;
514 ria->ria_pages = ras->ras_stride_pages;
516 spin_unlock(&ras->ras_lock);
519 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
522 len = ria_page_count(ria);
524 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
528 CDEBUG(D_READA, DFID ": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
529 PFID(lu_object_fid(&clob->co_lu)),
530 ria->ria_start, ria->ria_end,
531 vio->vui_ra_valid ? vio->vui_ra_start : 0,
532 vio->vui_ra_valid ? vio->vui_ra_count : 0,
535 /* at least to extend the readahead window to cover current read */
536 if (!hit && vio->vui_ra_valid &&
537 vio->vui_ra_start + vio->vui_ra_count > ria->ria_start) {
538 /* to the end of current read window. */
539 mlen = vio->vui_ra_start + vio->vui_ra_count - ria->ria_start;
540 /* trim to RPC boundary */
541 start = ria->ria_start & (PTLRPC_MAX_BRW_PAGES - 1);
542 mlen = min(mlen, PTLRPC_MAX_BRW_PAGES - start);
545 reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len, mlen);
547 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
549 CDEBUG(D_READA, "reserved pages %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
551 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
552 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
554 ret = ll_read_ahead_pages(env, io, queue, ria, &reserved, &ra_end);
557 ll_ra_count_put(ll_i2sbi(inode), reserved);
559 if (ra_end == end + 1 && ra_end == (kms >> PAGE_SHIFT))
560 ll_ra_stats_inc(inode, RA_STAT_EOF);
562 /* if we didn't get to the end of the region we reserved from
563 * the ras we need to go back and update the ras so that the
564 * next read-ahead tries from where we left off. we only do so
565 * if the region we failed to issue read-ahead on is still ahead
566 * of the app and behind the next index to start read-ahead from
568 CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu\n",
569 ra_end, end, ria->ria_end);
571 if (ra_end != end + 1) {
572 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
573 spin_lock(&ras->ras_lock);
574 if (ra_end < ras->ras_next_readahead &&
575 index_in_window(ra_end, ras->ras_window_start, 0,
576 ras->ras_window_len)) {
577 ras->ras_next_readahead = ra_end;
580 spin_unlock(&ras->ras_lock);
586 static void ras_set_start(struct inode *inode, struct ll_readahead_state *ras,
589 ras->ras_window_start = index & (~(RAS_INCREASE_STEP(inode) - 1));
592 /* called with the ras_lock held or from places where it doesn't matter */
593 static void ras_reset(struct inode *inode, struct ll_readahead_state *ras,
596 ras->ras_last_readpage = index;
597 ras->ras_consecutive_requests = 0;
598 ras->ras_consecutive_pages = 0;
599 ras->ras_window_len = 0;
600 ras_set_start(inode, ras, index);
601 ras->ras_next_readahead = max(ras->ras_window_start, index);
606 /* called with the ras_lock held or from places where it doesn't matter */
607 static void ras_stride_reset(struct ll_readahead_state *ras)
609 ras->ras_consecutive_stride_requests = 0;
610 ras->ras_stride_length = 0;
611 ras->ras_stride_pages = 0;
615 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
617 spin_lock_init(&ras->ras_lock);
618 ras_reset(inode, ras, 0);
619 ras->ras_requests = 0;
623 * Check whether the read request is in the stride window.
624 * If it is in the stride window, return 1, otherwise return 0.
626 static int index_in_stride_window(struct ll_readahead_state *ras,
629 unsigned long stride_gap;
631 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
632 ras->ras_stride_pages == ras->ras_stride_length)
635 stride_gap = index - ras->ras_last_readpage - 1;
637 /* If it is contiguous read */
639 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
641 /* Otherwise check the stride by itself */
642 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
643 ras->ras_consecutive_pages == ras->ras_stride_pages;
646 static void ras_update_stride_detector(struct ll_readahead_state *ras,
649 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
651 if ((stride_gap != 0 || ras->ras_consecutive_stride_requests == 0) &&
652 !stride_io_mode(ras)) {
653 ras->ras_stride_pages = ras->ras_consecutive_pages;
654 ras->ras_stride_length = ras->ras_consecutive_pages +
657 LASSERT(ras->ras_request_index == 0);
658 LASSERT(ras->ras_consecutive_stride_requests == 0);
660 if (index <= ras->ras_last_readpage) {
661 /*Reset stride window for forward read*/
662 ras_stride_reset(ras);
666 ras->ras_stride_pages = ras->ras_consecutive_pages;
667 ras->ras_stride_length = stride_gap + ras->ras_consecutive_pages;
673 /* Stride Read-ahead window will be increased inc_len according to
676 static void ras_stride_increase_window(struct ll_readahead_state *ras,
677 struct ll_ra_info *ra,
678 unsigned long inc_len)
680 unsigned long left, step, window_len;
681 unsigned long stride_len;
683 LASSERT(ras->ras_stride_length > 0);
684 LASSERTF(ras->ras_window_start + ras->ras_window_len
685 >= ras->ras_stride_offset, "window_start %lu, window_len %lu stride_offset %lu\n",
686 ras->ras_window_start,
687 ras->ras_window_len, ras->ras_stride_offset);
689 stride_len = ras->ras_window_start + ras->ras_window_len -
690 ras->ras_stride_offset;
692 left = stride_len % ras->ras_stride_length;
693 window_len = ras->ras_window_len - left;
695 if (left < ras->ras_stride_pages)
698 left = ras->ras_stride_pages + inc_len;
700 LASSERT(ras->ras_stride_pages != 0);
702 step = left / ras->ras_stride_pages;
703 left %= ras->ras_stride_pages;
705 window_len += step * ras->ras_stride_length + left;
707 if (stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
708 ras->ras_stride_pages, ras->ras_stride_offset,
709 window_len) <= ra->ra_max_pages_per_file)
710 ras->ras_window_len = window_len;
715 static void ras_increase_window(struct inode *inode,
716 struct ll_readahead_state *ras,
717 struct ll_ra_info *ra)
719 /* The stretch of ra-window should be aligned with max rpc_size
720 * but current clio architecture does not support retrieve such
721 * information from lower layer. FIXME later
723 if (stride_io_mode(ras))
724 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP(inode));
726 ras->ras_window_len = min(ras->ras_window_len +
727 RAS_INCREASE_STEP(inode),
728 ra->ra_max_pages_per_file);
731 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
732 struct ll_readahead_state *ras, unsigned long index,
735 struct ll_ra_info *ra = &sbi->ll_ra_info;
736 int zero = 0, stride_detect = 0, ra_miss = 0;
738 spin_lock(&ras->ras_lock);
740 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
742 /* reset the read-ahead window in two cases. First when the app seeks
743 * or reads to some other part of the file. Secondly if we get a
744 * read-ahead miss that we think we've previously issued. This can
745 * be a symptom of there being so many read-ahead pages that the VM is
746 * reclaiming it before we get to it.
748 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
750 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
751 } else if (!hit && ras->ras_window_len &&
752 index < ras->ras_next_readahead &&
753 index_in_window(index, ras->ras_window_start, 0,
754 ras->ras_window_len)) {
756 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
759 /* On the second access to a file smaller than the tunable
760 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
761 * file up to ra_max_pages_per_file. This is simply a best effort
762 * and only occurs once per open file. Normal RA behavior is reverted
763 * to for subsequent IO. The mmap case does not increment
764 * ras_requests and thus can never trigger this behavior.
766 if (ras->ras_requests == 2 && !ras->ras_request_index) {
769 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
772 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
773 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
776 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
777 ras->ras_window_start = 0;
778 ras->ras_last_readpage = 0;
779 ras->ras_next_readahead = 0;
780 ras->ras_window_len = min(ra->ra_max_pages_per_file,
781 ra->ra_max_read_ahead_whole_pages);
786 /* check whether it is in stride I/O mode*/
787 if (!index_in_stride_window(ras, index)) {
788 if (ras->ras_consecutive_stride_requests == 0 &&
789 ras->ras_request_index == 0) {
790 ras_update_stride_detector(ras, index);
791 ras->ras_consecutive_stride_requests++;
793 ras_stride_reset(ras);
795 ras_reset(inode, ras, index);
796 ras->ras_consecutive_pages++;
799 ras->ras_consecutive_pages = 0;
800 ras->ras_consecutive_requests = 0;
801 if (++ras->ras_consecutive_stride_requests > 1)
807 if (index_in_stride_window(ras, index) &&
808 stride_io_mode(ras)) {
809 /*If stride-RA hit cache miss, the stride dector
810 *will not be reset to avoid the overhead of
811 *redetecting read-ahead mode
813 if (index != ras->ras_last_readpage + 1)
814 ras->ras_consecutive_pages = 0;
815 ras_reset(inode, ras, index);
818 /* Reset both stride window and normal RA
821 ras_reset(inode, ras, index);
822 ras->ras_consecutive_pages++;
823 ras_stride_reset(ras);
826 } else if (stride_io_mode(ras)) {
827 /* If this is contiguous read but in stride I/O mode
828 * currently, check whether stride step still is valid,
829 * if invalid, it will reset the stride ra window
831 if (!index_in_stride_window(ras, index)) {
832 /* Shrink stride read-ahead window to be zero */
833 ras_stride_reset(ras);
834 ras->ras_window_len = 0;
835 ras->ras_next_readahead = index;
839 ras->ras_consecutive_pages++;
840 ras->ras_last_readpage = index;
841 ras_set_start(inode, ras, index);
843 if (stride_io_mode(ras)) {
844 /* Since stride readahead is sensitive to the offset
845 * of read-ahead, so we use original offset here,
846 * instead of ras_window_start, which is RPC aligned
848 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
850 if (ras->ras_next_readahead < ras->ras_window_start)
851 ras->ras_next_readahead = ras->ras_window_start;
853 ras->ras_next_readahead = index + 1;
857 /* Trigger RA in the mmap case where ras_consecutive_requests
858 * is not incremented and thus can't be used to trigger RA
860 if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
861 ras->ras_window_len = RAS_INCREASE_STEP(inode);
865 /* Initially reset the stride window offset to next_readahead*/
866 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
868 * Once stride IO mode is detected, next_readahead should be
869 * reset to make sure next_readahead > stride offset
871 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
872 ras->ras_stride_offset = index;
873 ras->ras_window_len = RAS_INCREASE_STEP(inode);
876 /* The initial ras_window_len is set to the request size. To avoid
877 * uselessly reading and discarding pages for random IO the window is
878 * only increased once per consecutive request received. */
879 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
880 !ras->ras_request_index)
881 ras_increase_window(inode, ras, ra);
884 ras->ras_request_index++;
885 spin_unlock(&ras->ras_lock);
889 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
891 struct inode *inode = vmpage->mapping->host;
892 struct ll_inode_info *lli = ll_i2info(inode);
895 struct cl_page *page;
896 struct cl_object *clob;
897 struct cl_env_nest nest;
898 bool redirtied = false;
899 bool unlocked = false;
902 LASSERT(PageLocked(vmpage));
903 LASSERT(!PageWriteback(vmpage));
905 LASSERT(ll_i2dtexp(inode));
907 env = cl_env_nested_get(&nest);
909 result = PTR_ERR(env);
913 clob = ll_i2info(inode)->lli_clob;
916 io = vvp_env_thread_io(env);
918 io->ci_ignore_layout = 1;
919 result = cl_io_init(env, io, CIT_MISC, clob);
921 page = cl_page_find(env, clob, vmpage->index,
922 vmpage, CPT_CACHEABLE);
924 lu_ref_add(&page->cp_reference, "writepage",
926 cl_page_assume(env, io, page);
927 result = cl_page_flush(env, io, page);
930 * Re-dirty page on error so it retries write,
931 * but not in case when IO has actually
932 * occurred and completed with an error.
934 if (!PageError(vmpage)) {
935 redirty_page_for_writepage(wbc, vmpage);
940 cl_page_disown(env, io, page);
942 lu_ref_del(&page->cp_reference,
943 "writepage", current);
944 cl_page_put(env, page);
946 result = PTR_ERR(page);
951 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
952 loff_t offset = cl_offset(clob, vmpage->index);
954 /* Flush page failed because the extent is being written out.
955 * Wait for the write of extent to be finished to avoid
956 * breaking kernel which assumes ->writepage should mark
957 * PageWriteback or clean the page.
959 result = cl_sync_file_range(inode, offset,
960 offset + PAGE_SIZE - 1,
963 /* actually we may have written more than one page.
964 * decreasing this page because the caller will count
967 wbc->nr_to_write -= result - 1;
972 cl_env_nested_put(&nest, env);
977 if (!lli->lli_async_rc)
978 lli->lli_async_rc = result;
979 SetPageError(vmpage);
986 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
988 struct inode *inode = mapping->host;
989 struct ll_sb_info *sbi = ll_i2sbi(inode);
992 enum cl_fsync_mode mode;
995 int ignore_layout = 0;
997 if (wbc->range_cyclic) {
998 start = mapping->writeback_index << PAGE_SHIFT;
999 end = OBD_OBJECT_EOF;
1001 start = wbc->range_start;
1002 end = wbc->range_end;
1003 if (end == LLONG_MAX) {
1004 end = OBD_OBJECT_EOF;
1005 range_whole = start == 0;
1009 mode = CL_FSYNC_NONE;
1010 if (wbc->sync_mode == WB_SYNC_ALL)
1011 mode = CL_FSYNC_LOCAL;
1013 if (sbi->ll_umounting)
1014 /* if the mountpoint is being umounted, all pages have to be
1015 * evicted to avoid hitting LBUG when truncate_inode_pages()
1016 * is called later on.
1020 if (!ll_i2info(inode)->lli_clob)
1023 result = cl_sync_file_range(inode, start, end, mode, ignore_layout);
1025 wbc->nr_to_write -= result;
1029 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1030 if (end == OBD_OBJECT_EOF)
1031 mapping->writeback_index = 0;
1033 mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
1038 struct ll_cl_context *ll_cl_find(struct file *file)
1040 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1041 struct ll_cl_context *lcc;
1042 struct ll_cl_context *found = NULL;
1044 read_lock(&fd->fd_lock);
1045 list_for_each_entry(lcc, &fd->fd_lccs, lcc_list) {
1046 if (lcc->lcc_cookie == current) {
1051 read_unlock(&fd->fd_lock);
1056 void ll_cl_add(struct file *file, const struct lu_env *env, struct cl_io *io)
1058 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1059 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1061 memset(lcc, 0, sizeof(*lcc));
1062 INIT_LIST_HEAD(&lcc->lcc_list);
1063 lcc->lcc_cookie = current;
1067 write_lock(&fd->fd_lock);
1068 list_add(&lcc->lcc_list, &fd->fd_lccs);
1069 write_unlock(&fd->fd_lock);
1072 void ll_cl_remove(struct file *file, const struct lu_env *env)
1074 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1075 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1077 write_lock(&fd->fd_lock);
1078 list_del_init(&lcc->lcc_list);
1079 write_unlock(&fd->fd_lock);
1082 int ll_readpage(struct file *file, struct page *vmpage)
1084 struct cl_object *clob = ll_i2info(file_inode(file))->lli_clob;
1085 struct ll_cl_context *lcc;
1086 const struct lu_env *env;
1088 struct cl_page *page;
1091 lcc = ll_cl_find(file);
1093 unlock_page(vmpage);
1099 LASSERT(io->ci_state == CIS_IO_GOING);
1100 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
1101 if (!IS_ERR(page)) {
1102 LASSERT(page->cp_type == CPT_CACHEABLE);
1103 if (likely(!PageUptodate(vmpage))) {
1104 cl_page_assume(env, io, page);
1105 result = cl_io_read_page(env, io, page);
1107 /* Page from a non-object file. */
1108 unlock_page(vmpage);
1111 cl_page_put(env, page);
1113 unlock_page(vmpage);
1114 result = PTR_ERR(page);
1119 int ll_page_sync_io(const struct lu_env *env, struct cl_io *io,
1120 struct cl_page *page, enum cl_req_type crt)
1122 struct cl_2queue *queue;
1125 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
1127 queue = &io->ci_queue;
1128 cl_2queue_init_page(queue, page);
1130 result = cl_io_submit_sync(env, io, crt, queue, 0);
1131 LASSERT(cl_page_is_owned(page, io));
1133 if (crt == CRT_READ)
1135 * in CRT_WRITE case page is left locked even in case of
1138 cl_page_list_disown(env, io, &queue->c2_qin);
1139 cl_2queue_fini(env, queue);