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"
63 * Finalizes cl-data before exiting typical address_space operation. Dual to
66 void ll_cl_fini(struct ll_cl_context *lcc)
68 struct lu_env *env = lcc->lcc_env;
69 struct cl_io *io = lcc->lcc_io;
70 struct cl_page *page = lcc->lcc_page;
72 LASSERT(lcc->lcc_cookie == current);
76 lu_ref_del(&page->cp_reference, "cl_io", io);
77 cl_page_put(env, page);
80 cl_env_put(env, &lcc->lcc_refcheck);
84 * Initializes common cl-data at the typical address_space operation entry
87 struct ll_cl_context *ll_cl_init(struct file *file, struct page *vmpage)
89 struct ll_cl_context *lcc;
92 struct cl_object *clob;
98 clob = ll_i2info(file_inode(file))->lli_clob;
101 env = cl_env_get(&refcheck);
103 return ERR_CAST(env);
105 lcc = &ll_env_info(env)->lti_io_ctx;
106 memset(lcc, 0, sizeof(*lcc));
108 lcc->lcc_refcheck = refcheck;
109 lcc->lcc_cookie = current;
111 vio = vvp_env_io(env);
112 io = vio->vui_cl.cis_io;
117 if (result == 0 && vmpage) {
118 struct cl_page *page;
120 LASSERT(io->ci_state == CIS_IO_GOING);
121 LASSERT(vio->vui_fd == LUSTRE_FPRIVATE(file));
122 page = cl_page_find(env, clob, vmpage->index, vmpage,
125 lcc->lcc_page = page;
126 lu_ref_add(&page->cp_reference, "cl_io", io);
129 result = PTR_ERR(page);
134 lcc = ERR_PTR(result);
140 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
143 * Get readahead pages from the filesystem readahead pool of the client for a
146 * /param sbi superblock for filesystem readahead state ll_ra_info
147 * /param ria per-thread readahead state
148 * /param pages number of pages requested for readahead for the thread.
150 * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
151 * It should work well if the ra_max_pages is much greater than the single
152 * file's read-ahead window, and not too many threads contending for
153 * these readahead pages.
155 * TODO: There may be a 'global sync problem' if many threads are trying
156 * to get an ra budget that is larger than the remaining readahead pages
157 * and reach here at exactly the same time. They will compute /a ret to
158 * consume the remaining pages, but will fail at atomic_add_return() and
159 * get a zero ra window, although there is still ra space remaining. - Jay
161 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
162 struct ra_io_arg *ria,
163 unsigned long pages, unsigned long min)
165 struct ll_ra_info *ra = &sbi->ll_ra_info;
168 /* If read-ahead pages left are less than 1M, do not do read-ahead,
169 * otherwise it will form small read RPC(< 1M), which hurt server
172 ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages), pages);
173 if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages)) {
178 /* If the non-strided (ria_pages == 0) readahead window
179 * (ria_start + ret) has grown across an RPC boundary, then trim
180 * readahead size by the amount beyond the RPC so it ends on an
181 * RPC boundary. If the readahead window is already ending on
182 * an RPC boundary (beyond_rpc == 0), or smaller than a full
183 * RPC (beyond_rpc < ret) the readahead size is unchanged.
184 * The (beyond_rpc != 0) check is skipped since the conditional
185 * branch is more expensive than subtracting zero from the result.
187 * Strided read is left unaligned to avoid small fragments beyond
188 * the RPC boundary from needing an extra read RPC.
190 if (ria->ria_pages == 0) {
191 long beyond_rpc = (ria->ria_start + ret) % PTLRPC_MAX_BRW_PAGES;
193 if (/* beyond_rpc != 0 && */ beyond_rpc < ret)
197 if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
198 atomic_sub(ret, &ra->ra_cur_pages);
204 /* override ra limit for maximum performance */
205 atomic_add(min - ret, &ra->ra_cur_pages);
211 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
213 struct ll_ra_info *ra = &sbi->ll_ra_info;
215 atomic_sub(len, &ra->ra_cur_pages);
218 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
220 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
221 lprocfs_counter_incr(sbi->ll_ra_stats, which);
224 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
226 struct ll_sb_info *sbi = ll_i2sbi(inode);
228 ll_ra_stats_inc_sbi(sbi, which);
231 #define RAS_CDEBUG(ras) \
233 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu" \
234 "csr %lu sf %lu sp %lu sl %lu\n", \
235 ras->ras_last_readpage, ras->ras_consecutive_requests, \
236 ras->ras_consecutive_pages, ras->ras_window_start, \
237 ras->ras_window_len, ras->ras_next_readahead, \
238 ras->ras_requests, ras->ras_request_index, \
239 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
240 ras->ras_stride_pages, ras->ras_stride_length)
242 static int index_in_window(unsigned long index, unsigned long point,
243 unsigned long before, unsigned long after)
245 unsigned long start = point - before, end = point + after;
252 return start <= index && index <= end;
255 void ll_ras_enter(struct file *f)
257 struct ll_file_data *fd = LUSTRE_FPRIVATE(f);
258 struct ll_readahead_state *ras = &fd->fd_ras;
260 spin_lock(&ras->ras_lock);
262 ras->ras_request_index = 0;
263 ras->ras_consecutive_requests++;
264 spin_unlock(&ras->ras_lock);
267 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
268 struct cl_page_list *queue, struct cl_page *page,
269 struct cl_object *clob, pgoff_t *max_index)
271 struct page *vmpage = page->cp_vmpage;
272 struct vvp_page *vpg;
276 cl_page_assume(env, io, page);
277 lu_ref_add(&page->cp_reference, "ra", current);
278 vpg = cl2vvp_page(cl_object_page_slice(clob, page));
279 if (!vpg->vpg_defer_uptodate && !PageUptodate(vmpage)) {
280 CDEBUG(D_READA, "page index %lu, max_index: %lu\n",
281 vvp_index(vpg), *max_index);
282 if (*max_index == 0 || vvp_index(vpg) > *max_index)
283 rc = cl_page_is_under_lock(env, io, page, max_index);
285 vpg->vpg_defer_uptodate = 1;
286 vpg->vpg_ra_used = 0;
287 cl_page_list_add(queue, page);
290 cl_page_discard(env, io, page);
294 /* skip completed pages */
295 cl_page_unassume(env, io, page);
297 lu_ref_del(&page->cp_reference, "ra", current);
298 cl_page_put(env, page);
303 * Initiates read-ahead of a page with given index.
305 * \retval +ve: page was added to \a queue.
307 * \retval -ENOLCK: there is no extent lock for this part of a file, stop
310 * \retval -ve, 0: page wasn't added to \a queue for other reason.
312 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
313 struct cl_page_list *queue,
314 pgoff_t index, pgoff_t *max_index)
316 struct cl_object *clob = io->ci_obj;
317 struct inode *inode = vvp_object_inode(clob);
319 struct cl_page *page;
320 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
322 const char *msg = NULL;
324 vmpage = grab_cache_page_nowait(inode->i_mapping, index);
326 /* Check if vmpage was truncated or reclaimed */
327 if (vmpage->mapping == inode->i_mapping) {
328 page = cl_page_find(env, clob, vmpage->index,
329 vmpage, CPT_CACHEABLE);
331 rc = cl_read_ahead_page(env, io, queue,
332 page, clob, max_index);
334 which = RA_STAT_FAILED_MATCH;
335 msg = "lock match failed";
338 which = RA_STAT_FAILED_GRAB_PAGE;
339 msg = "cl_page_find failed";
342 which = RA_STAT_WRONG_GRAB_PAGE;
343 msg = "g_c_p_n returned invalid page";
349 which = RA_STAT_FAILED_GRAB_PAGE;
350 msg = "g_c_p_n failed";
353 ll_ra_stats_inc(inode, which);
354 CDEBUG(D_READA, "%s\n", msg);
359 #define RIA_DEBUG(ria) \
360 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
361 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
364 /* Limit this to the blocksize instead of PTLRPC_BRW_MAX_SIZE, since we don't
365 * know what the actual RPC size is. If this needs to change, it makes more
366 * sense to tune the i_blkbits value for the file based on the OSTs it is
367 * striped over, rather than having a constant value for all files here.
370 /* RAS_INCREASE_STEP should be (1UL << (inode->i_blkbits - PAGE_SHIFT)).
371 * Temporarily set RAS_INCREASE_STEP to 1MB. After 4MB RPC is enabled
372 * by default, this should be adjusted corresponding with max_read_ahead_mb
373 * and max_read_ahead_per_file_mb otherwise the readahead budget can be used
374 * up quickly which will affect read performance significantly. See LU-2816
376 #define RAS_INCREASE_STEP(inode) (ONE_MB_BRW_SIZE >> PAGE_SHIFT)
378 static inline int stride_io_mode(struct ll_readahead_state *ras)
380 return ras->ras_consecutive_stride_requests > 1;
383 /* The function calculates how much pages will be read in
384 * [off, off + length], in such stride IO area,
385 * stride_offset = st_off, stride_length = st_len,
386 * stride_pages = st_pgs
388 * |------------------|*****|------------------|*****|------------|*****|....
391 * |----- st_len -----|
393 * How many pages it should read in such pattern
394 * |-------------------------------------------------------------|
396 * |<------ length ------->|
398 * = |<----->| + |-------------------------------------| + |---|
399 * start_left st_pgs * i end_left
402 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
403 unsigned long off, unsigned long length)
405 __u64 start = off > st_off ? off - st_off : 0;
406 __u64 end = off + length > st_off ? off + length - st_off : 0;
407 unsigned long start_left = 0;
408 unsigned long end_left = 0;
409 unsigned long pg_count;
411 if (st_len == 0 || length == 0 || end == 0)
414 start_left = do_div(start, st_len);
415 if (start_left < st_pgs)
416 start_left = st_pgs - start_left;
420 end_left = do_div(end, st_len);
421 if (end_left > st_pgs)
424 CDEBUG(D_READA, "start %llu, end %llu start_left %lu end_left %lu\n",
425 start, end, start_left, end_left);
428 pg_count = end_left - (st_pgs - start_left);
430 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
432 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu pgcount %lu\n",
433 st_off, st_len, st_pgs, off, length, pg_count);
438 static int ria_page_count(struct ra_io_arg *ria)
440 __u64 length = ria->ria_end >= ria->ria_start ?
441 ria->ria_end - ria->ria_start + 1 : 0;
443 return stride_pg_count(ria->ria_stoff, ria->ria_length,
444 ria->ria_pages, ria->ria_start,
448 /*Check whether the index is in the defined ra-window */
449 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
451 /* If ria_length == ria_pages, it means non-stride I/O mode,
452 * idx should always inside read-ahead window in this case
453 * For stride I/O mode, just check whether the idx is inside
456 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
457 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
458 ria->ria_length < ria->ria_pages);
461 static int ll_read_ahead_pages(const struct lu_env *env,
462 struct cl_io *io, struct cl_page_list *queue,
463 struct ra_io_arg *ria,
464 unsigned long *reserved_pages,
465 unsigned long *ra_end)
470 pgoff_t max_index = 0;
475 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
476 for (page_idx = ria->ria_start;
477 page_idx <= ria->ria_end && *reserved_pages > 0; page_idx++) {
478 if (ras_inside_ra_window(page_idx, ria)) {
479 /* If the page is inside the read-ahead window*/
480 rc = ll_read_ahead_page(env, io, queue,
481 page_idx, &max_index);
485 } else if (rc == -ENOLCK) {
488 } else if (stride_ria) {
489 /* If it is not in the read-ahead window, and it is
490 * read-ahead mode, then check whether it should skip
494 /* FIXME: This assertion only is valid when it is for
495 * forward read-ahead, it will be fixed when backward
496 * read-ahead is implemented
498 LASSERTF(page_idx > ria->ria_stoff, "Invalid page_idx %lu rs %lu re %lu ro %lu rl %lu rp %lu\n",
500 ria->ria_start, ria->ria_end, ria->ria_stoff,
501 ria->ria_length, ria->ria_pages);
502 offset = page_idx - ria->ria_stoff;
503 offset = offset % (ria->ria_length);
504 if (offset > ria->ria_pages) {
505 page_idx += ria->ria_length - offset;
506 CDEBUG(D_READA, "i %lu skip %lu\n", page_idx,
507 ria->ria_length - offset);
516 int ll_readahead(const struct lu_env *env, struct cl_io *io,
517 struct cl_page_list *queue, struct ll_readahead_state *ras,
520 struct vvp_io *vio = vvp_env_io(env);
521 struct ll_thread_info *lti = ll_env_info(env);
522 struct cl_attr *attr = vvp_env_thread_attr(env);
523 unsigned long start = 0, end = 0, reserved;
524 unsigned long ra_end, len, mlen = 0;
526 struct ra_io_arg *ria = <i->lti_ria;
527 struct cl_object *clob;
532 inode = vvp_object_inode(clob);
534 memset(ria, 0, sizeof(*ria));
536 cl_object_attr_lock(clob);
537 ret = cl_object_attr_get(env, clob, attr);
538 cl_object_attr_unlock(clob);
544 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
548 spin_lock(&ras->ras_lock);
550 /* Enlarge the RA window to encompass the full read */
551 if (vio->vui_ra_valid &&
552 ras->ras_window_start + ras->ras_window_len <
553 vio->vui_ra_start + vio->vui_ra_count) {
554 ras->ras_window_len = vio->vui_ra_start + vio->vui_ra_count -
555 ras->ras_window_start;
558 /* Reserve a part of the read-ahead window that we'll be issuing */
559 if (ras->ras_window_len) {
560 start = ras->ras_next_readahead;
561 end = ras->ras_window_start + ras->ras_window_len - 1;
564 unsigned long rpc_boundary;
566 * Align RA window to an optimal boundary.
568 * XXX This would be better to align to cl_max_pages_per_rpc
569 * instead of PTLRPC_MAX_BRW_PAGES, because the RPC size may
570 * be aligned to the RAID stripe size in the future and that
571 * is more important than the RPC size.
573 /* Note: we only trim the RPC, instead of extending the RPC
574 * to the boundary, so to avoid reading too much pages during
577 rpc_boundary = (end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1));
578 if (rpc_boundary > 0)
581 if (rpc_boundary > start)
584 /* Truncate RA window to end of file */
585 end = min(end, (unsigned long)((kms - 1) >> PAGE_SHIFT));
587 ras->ras_next_readahead = max(end, end + 1);
590 ria->ria_start = start;
592 /* If stride I/O mode is detected, get stride window*/
593 if (stride_io_mode(ras)) {
594 ria->ria_stoff = ras->ras_stride_offset;
595 ria->ria_length = ras->ras_stride_length;
596 ria->ria_pages = ras->ras_stride_pages;
598 spin_unlock(&ras->ras_lock);
601 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
604 len = ria_page_count(ria);
606 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
610 CDEBUG(D_READA, DFID ": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
611 PFID(lu_object_fid(&clob->co_lu)),
612 ria->ria_start, ria->ria_end,
613 vio->vui_ra_valid ? vio->vui_ra_start : 0,
614 vio->vui_ra_valid ? vio->vui_ra_count : 0,
617 /* at least to extend the readahead window to cover current read */
618 if (!hit && vio->vui_ra_valid &&
619 vio->vui_ra_start + vio->vui_ra_count > ria->ria_start) {
620 /* to the end of current read window. */
621 mlen = vio->vui_ra_start + vio->vui_ra_count - ria->ria_start;
622 /* trim to RPC boundary */
623 start = ria->ria_start & (PTLRPC_MAX_BRW_PAGES - 1);
624 mlen = min(mlen, PTLRPC_MAX_BRW_PAGES - start);
627 reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len, mlen);
629 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
631 CDEBUG(D_READA, "reserved pages %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
633 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
634 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
636 ret = ll_read_ahead_pages(env, io, queue, ria, &reserved, &ra_end);
639 ll_ra_count_put(ll_i2sbi(inode), reserved);
641 if (ra_end == end + 1 && ra_end == (kms >> PAGE_SHIFT))
642 ll_ra_stats_inc(inode, RA_STAT_EOF);
644 /* if we didn't get to the end of the region we reserved from
645 * the ras we need to go back and update the ras so that the
646 * next read-ahead tries from where we left off. we only do so
647 * if the region we failed to issue read-ahead on is still ahead
648 * of the app and behind the next index to start read-ahead from
650 CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu\n",
651 ra_end, end, ria->ria_end);
653 if (ra_end != end + 1) {
654 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
655 spin_lock(&ras->ras_lock);
656 if (ra_end < ras->ras_next_readahead &&
657 index_in_window(ra_end, ras->ras_window_start, 0,
658 ras->ras_window_len)) {
659 ras->ras_next_readahead = ra_end;
662 spin_unlock(&ras->ras_lock);
668 static void ras_set_start(struct inode *inode, struct ll_readahead_state *ras,
671 ras->ras_window_start = index & (~(RAS_INCREASE_STEP(inode) - 1));
674 /* called with the ras_lock held or from places where it doesn't matter */
675 static void ras_reset(struct inode *inode, struct ll_readahead_state *ras,
678 ras->ras_last_readpage = index;
679 ras->ras_consecutive_requests = 0;
680 ras->ras_consecutive_pages = 0;
681 ras->ras_window_len = 0;
682 ras_set_start(inode, ras, index);
683 ras->ras_next_readahead = max(ras->ras_window_start, index);
688 /* called with the ras_lock held or from places where it doesn't matter */
689 static void ras_stride_reset(struct ll_readahead_state *ras)
691 ras->ras_consecutive_stride_requests = 0;
692 ras->ras_stride_length = 0;
693 ras->ras_stride_pages = 0;
697 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
699 spin_lock_init(&ras->ras_lock);
700 ras_reset(inode, ras, 0);
701 ras->ras_requests = 0;
705 * Check whether the read request is in the stride window.
706 * If it is in the stride window, return 1, otherwise return 0.
708 static int index_in_stride_window(struct ll_readahead_state *ras,
711 unsigned long stride_gap;
713 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
714 ras->ras_stride_pages == ras->ras_stride_length)
717 stride_gap = index - ras->ras_last_readpage - 1;
719 /* If it is contiguous read */
721 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
723 /* Otherwise check the stride by itself */
724 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
725 ras->ras_consecutive_pages == ras->ras_stride_pages;
728 static void ras_update_stride_detector(struct ll_readahead_state *ras,
731 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
733 if (!stride_io_mode(ras) && (stride_gap != 0 ||
734 ras->ras_consecutive_stride_requests == 0)) {
735 ras->ras_stride_pages = ras->ras_consecutive_pages;
736 ras->ras_stride_length = stride_gap+ras->ras_consecutive_pages;
738 LASSERT(ras->ras_request_index == 0);
739 LASSERT(ras->ras_consecutive_stride_requests == 0);
741 if (index <= ras->ras_last_readpage) {
742 /*Reset stride window for forward read*/
743 ras_stride_reset(ras);
747 ras->ras_stride_pages = ras->ras_consecutive_pages;
748 ras->ras_stride_length = stride_gap+ras->ras_consecutive_pages;
754 /* Stride Read-ahead window will be increased inc_len according to
757 static void ras_stride_increase_window(struct ll_readahead_state *ras,
758 struct ll_ra_info *ra,
759 unsigned long inc_len)
761 unsigned long left, step, window_len;
762 unsigned long stride_len;
764 LASSERT(ras->ras_stride_length > 0);
765 LASSERTF(ras->ras_window_start + ras->ras_window_len
766 >= ras->ras_stride_offset, "window_start %lu, window_len %lu stride_offset %lu\n",
767 ras->ras_window_start,
768 ras->ras_window_len, ras->ras_stride_offset);
770 stride_len = ras->ras_window_start + ras->ras_window_len -
771 ras->ras_stride_offset;
773 left = stride_len % ras->ras_stride_length;
774 window_len = ras->ras_window_len - left;
776 if (left < ras->ras_stride_pages)
779 left = ras->ras_stride_pages + inc_len;
781 LASSERT(ras->ras_stride_pages != 0);
783 step = left / ras->ras_stride_pages;
784 left %= ras->ras_stride_pages;
786 window_len += step * ras->ras_stride_length + left;
788 if (stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
789 ras->ras_stride_pages, ras->ras_stride_offset,
790 window_len) <= ra->ra_max_pages_per_file)
791 ras->ras_window_len = window_len;
796 static void ras_increase_window(struct inode *inode,
797 struct ll_readahead_state *ras,
798 struct ll_ra_info *ra)
800 /* The stretch of ra-window should be aligned with max rpc_size
801 * but current clio architecture does not support retrieve such
802 * information from lower layer. FIXME later
804 if (stride_io_mode(ras))
805 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP(inode));
807 ras->ras_window_len = min(ras->ras_window_len +
808 RAS_INCREASE_STEP(inode),
809 ra->ra_max_pages_per_file);
812 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
813 struct ll_readahead_state *ras, unsigned long index,
816 struct ll_ra_info *ra = &sbi->ll_ra_info;
817 int zero = 0, stride_detect = 0, ra_miss = 0;
819 spin_lock(&ras->ras_lock);
821 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
823 /* reset the read-ahead window in two cases. First when the app seeks
824 * or reads to some other part of the file. Secondly if we get a
825 * read-ahead miss that we think we've previously issued. This can
826 * be a symptom of there being so many read-ahead pages that the VM is
827 * reclaiming it before we get to it.
829 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
831 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
832 } else if (!hit && ras->ras_window_len &&
833 index < ras->ras_next_readahead &&
834 index_in_window(index, ras->ras_window_start, 0,
835 ras->ras_window_len)) {
837 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
840 /* On the second access to a file smaller than the tunable
841 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
842 * file up to ra_max_pages_per_file. This is simply a best effort
843 * and only occurs once per open file. Normal RA behavior is reverted
844 * to for subsequent IO. The mmap case does not increment
845 * ras_requests and thus can never trigger this behavior.
847 if (ras->ras_requests == 2 && !ras->ras_request_index) {
850 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
853 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
854 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
857 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
858 ras->ras_window_start = 0;
859 ras->ras_last_readpage = 0;
860 ras->ras_next_readahead = 0;
861 ras->ras_window_len = min(ra->ra_max_pages_per_file,
862 ra->ra_max_read_ahead_whole_pages);
867 /* check whether it is in stride I/O mode*/
868 if (!index_in_stride_window(ras, index)) {
869 if (ras->ras_consecutive_stride_requests == 0 &&
870 ras->ras_request_index == 0) {
871 ras_update_stride_detector(ras, index);
872 ras->ras_consecutive_stride_requests++;
874 ras_stride_reset(ras);
876 ras_reset(inode, ras, index);
877 ras->ras_consecutive_pages++;
880 ras->ras_consecutive_pages = 0;
881 ras->ras_consecutive_requests = 0;
882 if (++ras->ras_consecutive_stride_requests > 1)
888 if (index_in_stride_window(ras, index) &&
889 stride_io_mode(ras)) {
890 /*If stride-RA hit cache miss, the stride dector
891 *will not be reset to avoid the overhead of
892 *redetecting read-ahead mode
894 if (index != ras->ras_last_readpage + 1)
895 ras->ras_consecutive_pages = 0;
896 ras_reset(inode, ras, index);
899 /* Reset both stride window and normal RA
902 ras_reset(inode, ras, index);
903 ras->ras_consecutive_pages++;
904 ras_stride_reset(ras);
907 } else if (stride_io_mode(ras)) {
908 /* If this is contiguous read but in stride I/O mode
909 * currently, check whether stride step still is valid,
910 * if invalid, it will reset the stride ra window
912 if (!index_in_stride_window(ras, index)) {
913 /* Shrink stride read-ahead window to be zero */
914 ras_stride_reset(ras);
915 ras->ras_window_len = 0;
916 ras->ras_next_readahead = index;
920 ras->ras_consecutive_pages++;
921 ras->ras_last_readpage = index;
922 ras_set_start(inode, ras, index);
924 if (stride_io_mode(ras)) {
925 /* Since stride readahead is sensitive to the offset
926 * of read-ahead, so we use original offset here,
927 * instead of ras_window_start, which is RPC aligned
929 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
931 if (ras->ras_next_readahead < ras->ras_window_start)
932 ras->ras_next_readahead = ras->ras_window_start;
934 ras->ras_next_readahead = index + 1;
938 /* Trigger RA in the mmap case where ras_consecutive_requests
939 * is not incremented and thus can't be used to trigger RA
941 if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
942 ras->ras_window_len = RAS_INCREASE_STEP(inode);
946 /* Initially reset the stride window offset to next_readahead*/
947 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
949 * Once stride IO mode is detected, next_readahead should be
950 * reset to make sure next_readahead > stride offset
952 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
953 ras->ras_stride_offset = index;
954 ras->ras_window_len = RAS_INCREASE_STEP(inode);
957 /* The initial ras_window_len is set to the request size. To avoid
958 * uselessly reading and discarding pages for random IO the window is
959 * only increased once per consecutive request received. */
960 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
961 !ras->ras_request_index)
962 ras_increase_window(inode, ras, ra);
965 ras->ras_request_index++;
966 spin_unlock(&ras->ras_lock);
970 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
972 struct inode *inode = vmpage->mapping->host;
973 struct ll_inode_info *lli = ll_i2info(inode);
976 struct cl_page *page;
977 struct cl_object *clob;
978 struct cl_env_nest nest;
979 bool redirtied = false;
980 bool unlocked = false;
983 LASSERT(PageLocked(vmpage));
984 LASSERT(!PageWriteback(vmpage));
986 LASSERT(ll_i2dtexp(inode));
988 env = cl_env_nested_get(&nest);
990 result = PTR_ERR(env);
994 clob = ll_i2info(inode)->lli_clob;
997 io = vvp_env_thread_io(env);
999 io->ci_ignore_layout = 1;
1000 result = cl_io_init(env, io, CIT_MISC, clob);
1002 page = cl_page_find(env, clob, vmpage->index,
1003 vmpage, CPT_CACHEABLE);
1004 if (!IS_ERR(page)) {
1005 lu_ref_add(&page->cp_reference, "writepage",
1007 cl_page_assume(env, io, page);
1008 result = cl_page_flush(env, io, page);
1011 * Re-dirty page on error so it retries write,
1012 * but not in case when IO has actually
1013 * occurred and completed with an error.
1015 if (!PageError(vmpage)) {
1016 redirty_page_for_writepage(wbc, vmpage);
1021 cl_page_disown(env, io, page);
1023 lu_ref_del(&page->cp_reference,
1024 "writepage", current);
1025 cl_page_put(env, page);
1027 result = PTR_ERR(page);
1030 cl_io_fini(env, io);
1032 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1033 loff_t offset = cl_offset(clob, vmpage->index);
1035 /* Flush page failed because the extent is being written out.
1036 * Wait for the write of extent to be finished to avoid
1037 * breaking kernel which assumes ->writepage should mark
1038 * PageWriteback or clean the page.
1040 result = cl_sync_file_range(inode, offset,
1041 offset + PAGE_SIZE - 1,
1044 /* actually we may have written more than one page.
1045 * decreasing this page because the caller will count
1048 wbc->nr_to_write -= result - 1;
1053 cl_env_nested_put(&nest, env);
1058 if (!lli->lli_async_rc)
1059 lli->lli_async_rc = result;
1060 SetPageError(vmpage);
1062 unlock_page(vmpage);
1067 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1069 struct inode *inode = mapping->host;
1070 struct ll_sb_info *sbi = ll_i2sbi(inode);
1073 enum cl_fsync_mode mode;
1074 int range_whole = 0;
1076 int ignore_layout = 0;
1078 if (wbc->range_cyclic) {
1079 start = mapping->writeback_index << PAGE_SHIFT;
1080 end = OBD_OBJECT_EOF;
1082 start = wbc->range_start;
1083 end = wbc->range_end;
1084 if (end == LLONG_MAX) {
1085 end = OBD_OBJECT_EOF;
1086 range_whole = start == 0;
1090 mode = CL_FSYNC_NONE;
1091 if (wbc->sync_mode == WB_SYNC_ALL)
1092 mode = CL_FSYNC_LOCAL;
1094 if (sbi->ll_umounting)
1095 /* if the mountpoint is being umounted, all pages have to be
1096 * evicted to avoid hitting LBUG when truncate_inode_pages()
1097 * is called later on.
1100 result = cl_sync_file_range(inode, start, end, mode, ignore_layout);
1102 wbc->nr_to_write -= result;
1106 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1107 if (end == OBD_OBJECT_EOF)
1108 mapping->writeback_index = 0;
1110 mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
1115 int ll_readpage(struct file *file, struct page *vmpage)
1117 struct ll_cl_context *lcc;
1120 lcc = ll_cl_init(file, vmpage);
1122 struct lu_env *env = lcc->lcc_env;
1123 struct cl_io *io = lcc->lcc_io;
1124 struct cl_page *page = lcc->lcc_page;
1126 LASSERT(page->cp_type == CPT_CACHEABLE);
1127 if (likely(!PageUptodate(vmpage))) {
1128 cl_page_assume(env, io, page);
1129 result = cl_io_read_page(env, io, page);
1131 /* Page from a non-object file. */
1132 unlock_page(vmpage);
1137 unlock_page(vmpage);
1138 result = PTR_ERR(lcc);
1143 int ll_page_sync_io(const struct lu_env *env, struct cl_io *io,
1144 struct cl_page *page, enum cl_req_type crt)
1146 struct cl_2queue *queue;
1149 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
1151 queue = &io->ci_queue;
1152 cl_2queue_init_page(queue, page);
1154 result = cl_io_submit_sync(env, io, crt, queue, 0);
1155 LASSERT(cl_page_is_owned(page, io));
1157 if (crt == CRT_READ)
1159 * in CRT_WRITE case page is left locked even in case of
1162 cl_page_list_disown(env, io, &queue->c2_qin);
1163 cl_2queue_fini(env, queue);