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, 2013, 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 <asm/uaccess.h>
51 #include <linux/stat.h>
52 #include <asm/uaccess.h>
54 #include <linux/pagemap.h>
55 /* current_is_kswapd() */
56 #include <linux/swap.h>
58 #define DEBUG_SUBSYSTEM S_LLITE
60 #include <lustre_lite.h>
61 #include <obd_cksum.h>
62 #include "llite_internal.h"
63 #include <linux/lustre_compat25.h>
66 * Finalizes cl-data before exiting typical address_space operation. Dual to
69 void ll_cl_fini(struct ll_cl_context *lcc)
71 struct lu_env *env = lcc->lcc_env;
72 struct cl_io *io = lcc->lcc_io;
73 struct cl_page *page = lcc->lcc_page;
75 LASSERT(lcc->lcc_cookie == current);
79 lu_ref_del(&page->cp_reference, "cl_io", io);
80 cl_page_put(env, page);
83 cl_env_put(env, &lcc->lcc_refcheck);
87 * Initializes common cl-data at the typical address_space operation entry
90 struct ll_cl_context *ll_cl_init(struct file *file, struct page *vmpage)
92 struct ll_cl_context *lcc;
95 struct cl_object *clob;
101 clob = ll_i2info(file->f_dentry->d_inode)->lli_clob;
102 LASSERT(clob != NULL);
104 env = cl_env_get(&refcheck);
106 return ERR_PTR(PTR_ERR(env));
108 lcc = &vvp_env_info(env)->vti_io_ctx;
109 memset(lcc, 0, sizeof(*lcc));
111 lcc->lcc_refcheck = refcheck;
112 lcc->lcc_cookie = current;
114 cio = ccc_env_io(env);
115 io = cio->cui_cl.cis_io;
119 if (result == 0 && vmpage != NULL) {
120 struct cl_page *page;
123 LASSERT(io->ci_state == CIS_IO_GOING);
124 LASSERT(cio->cui_fd == LUSTRE_FPRIVATE(file));
125 page = cl_page_find(env, clob, vmpage->index, vmpage,
128 lcc->lcc_page = page;
129 lu_ref_add(&page->cp_reference, "cl_io", io);
132 result = PTR_ERR(page);
136 lcc = ERR_PTR(result);
142 struct obd_capa *cl_capa_lookup(struct inode *inode, enum cl_req_type crt)
146 opc = crt == CRT_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
147 return ll_osscapa_get(inode, opc);
150 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
153 * Get readahead pages from the filesystem readahead pool of the client for a
156 * /param sbi superblock for filesystem readahead state ll_ra_info
157 * /param ria per-thread readahead state
158 * /param pages number of pages requested for readahead for the thread.
160 * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
161 * It should work well if the ra_max_pages is much greater than the single
162 * file's read-ahead window, and not too many threads contending for
163 * these readahead pages.
165 * TODO: There may be a 'global sync problem' if many threads are trying
166 * to get an ra budget that is larger than the remaining readahead pages
167 * and reach here at exactly the same time. They will compute /a ret to
168 * consume the remaining pages, but will fail at atomic_add_return() and
169 * get a zero ra window, although there is still ra space remaining. - Jay */
171 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
172 struct ra_io_arg *ria,
173 unsigned long pages, unsigned long min)
175 struct ll_ra_info *ra = &sbi->ll_ra_info;
179 /* If read-ahead pages left are less than 1M, do not do read-ahead,
180 * otherwise it will form small read RPC(< 1M), which hurt server
181 * performance a lot. */
182 ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages), pages);
183 if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages))
186 /* If the non-strided (ria_pages == 0) readahead window
187 * (ria_start + ret) has grown across an RPC boundary, then trim
188 * readahead size by the amount beyond the RPC so it ends on an
189 * RPC boundary. If the readahead window is already ending on
190 * an RPC boundary (beyond_rpc == 0), or smaller than a full
191 * RPC (beyond_rpc < ret) the readahead size is unchanged.
192 * The (beyond_rpc != 0) check is skipped since the conditional
193 * branch is more expensive than subtracting zero from the result.
195 * Strided read is left unaligned to avoid small fragments beyond
196 * the RPC boundary from needing an extra read RPC. */
197 if (ria->ria_pages == 0) {
198 long beyond_rpc = (ria->ria_start + ret) % PTLRPC_MAX_BRW_PAGES;
199 if (/* beyond_rpc != 0 && */ beyond_rpc < ret)
203 if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
204 atomic_sub(ret, &ra->ra_cur_pages);
210 /* override ra limit for maximum performance */
211 atomic_add(min - ret, &ra->ra_cur_pages);
217 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
219 struct ll_ra_info *ra = &sbi->ll_ra_info;
220 atomic_sub(len, &ra->ra_cur_pages);
223 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
225 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
226 lprocfs_counter_incr(sbi->ll_ra_stats, which);
229 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
231 struct ll_sb_info *sbi = ll_i2sbi(inode);
232 ll_ra_stats_inc_sbi(sbi, which);
235 #define RAS_CDEBUG(ras) \
237 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu" \
238 "csr %lu sf %lu sp %lu sl %lu \n", \
239 ras->ras_last_readpage, ras->ras_consecutive_requests, \
240 ras->ras_consecutive_pages, ras->ras_window_start, \
241 ras->ras_window_len, ras->ras_next_readahead, \
242 ras->ras_requests, ras->ras_request_index, \
243 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
244 ras->ras_stride_pages, ras->ras_stride_length)
246 static int index_in_window(unsigned long index, unsigned long point,
247 unsigned long before, unsigned long after)
249 unsigned long start = point - before, end = point + after;
256 return start <= index && index <= end;
259 static struct ll_readahead_state *ll_ras_get(struct file *f)
261 struct ll_file_data *fd;
263 fd = LUSTRE_FPRIVATE(f);
267 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar)
269 struct ll_readahead_state *ras;
273 spin_lock(&ras->ras_lock);
275 ras->ras_request_index = 0;
276 ras->ras_consecutive_requests++;
277 rar->lrr_reader = current;
279 list_add(&rar->lrr_linkage, &ras->ras_read_beads);
280 spin_unlock(&ras->ras_lock);
283 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
285 struct ll_readahead_state *ras;
289 spin_lock(&ras->ras_lock);
290 list_del_init(&rar->lrr_linkage);
291 spin_unlock(&ras->ras_lock);
294 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
296 struct ll_ra_read *scan;
298 list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
299 if (scan->lrr_reader == current)
305 struct ll_ra_read *ll_ra_read_get(struct file *f)
307 struct ll_readahead_state *ras;
308 struct ll_ra_read *bead;
312 spin_lock(&ras->ras_lock);
313 bead = ll_ra_read_get_locked(ras);
314 spin_unlock(&ras->ras_lock);
318 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
319 struct cl_page_list *queue, struct cl_page *page,
320 struct cl_object *clob, pgoff_t *max_index)
322 struct page *vmpage = page->cp_vmpage;
329 cl_page_assume(env, io, page);
330 lu_ref_add(&page->cp_reference, "ra", current);
331 cp = cl2ccc_page(cl_object_page_slice(clob, page));
332 if (!cp->cpg_defer_uptodate && !PageUptodate(vmpage)) {
333 CDEBUG(D_READA, "page index %lu, max_index: %lu\n",
334 ccc_index(cp), *max_index);
335 /* Disable the optimization on prefetching maximum readahead
336 * index because there is a race with lock cancellation. This
337 * optimization will be revived later.
338 * if (*max_index == 0 || ccc_index(cp) > *max_index) */
339 rc = cl_page_is_under_lock(env, io, page, max_index);
341 cp->cpg_defer_uptodate = 1;
343 cl_page_list_add(queue, page);
346 cl_page_discard(env, io, page);
350 /* skip completed pages */
351 cl_page_unassume(env, io, page);
353 lu_ref_del(&page->cp_reference, "ra", current);
354 cl_page_put(env, page);
359 * Initiates read-ahead of a page with given index.
361 * \retval +ve: page was added to \a queue.
363 * \retval -ENOLCK: there is no extent lock for this part of a file, stop
366 * \retval -ve, 0: page wasn't added to \a queue for other reason.
368 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
369 struct cl_page_list *queue,
370 pgoff_t index, pgoff_t *max_index)
372 struct cl_object *clob = io->ci_obj;
373 struct inode *inode = ccc_object_inode(clob);
375 struct cl_page *page;
376 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
377 unsigned int gfp_mask;
379 const char *msg = NULL;
383 gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
385 gfp_mask |= __GFP_NOWARN;
387 vmpage = grab_cache_page_nowait(inode->i_mapping, index);
388 if (vmpage != NULL) {
389 /* Check if vmpage was truncated or reclaimed */
390 if (vmpage->mapping == inode->i_mapping) {
391 page = cl_page_find(env, clob, vmpage->index,
392 vmpage, CPT_CACHEABLE);
394 rc = cl_read_ahead_page(env, io, queue,
395 page, clob, max_index);
397 which = RA_STAT_FAILED_MATCH;
398 msg = "lock match failed";
401 which = RA_STAT_FAILED_GRAB_PAGE;
402 msg = "cl_page_find failed";
405 which = RA_STAT_WRONG_GRAB_PAGE;
406 msg = "g_c_p_n returned invalid page";
410 page_cache_release(vmpage);
412 which = RA_STAT_FAILED_GRAB_PAGE;
413 msg = "g_c_p_n failed";
416 ll_ra_stats_inc(inode, which);
417 CDEBUG(D_READA, "%s\n", msg);
422 #define RIA_DEBUG(ria) \
423 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
424 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
427 /* Limit this to the blocksize instead of PTLRPC_BRW_MAX_SIZE, since we don't
428 * know what the actual RPC size is. If this needs to change, it makes more
429 * sense to tune the i_blkbits value for the file based on the OSTs it is
430 * striped over, rather than having a constant value for all files here. */
432 /* RAS_INCREASE_STEP should be (1UL << (inode->i_blkbits - PAGE_CACHE_SHIFT)).
433 * Temprarily set RAS_INCREASE_STEP to 1MB. After 4MB RPC is enabled
434 * by default, this should be adjusted corresponding with max_read_ahead_mb
435 * and max_read_ahead_per_file_mb otherwise the readahead budget can be used
436 * up quickly which will affect read performance siginificantly. See LU-2816 */
437 #define RAS_INCREASE_STEP(inode) (ONE_MB_BRW_SIZE >> PAGE_CACHE_SHIFT)
439 static inline int stride_io_mode(struct ll_readahead_state *ras)
441 return ras->ras_consecutive_stride_requests > 1;
443 /* The function calculates how much pages will be read in
444 * [off, off + length], in such stride IO area,
445 * stride_offset = st_off, stride_lengh = st_len,
446 * stride_pages = st_pgs
448 * |------------------|*****|------------------|*****|------------|*****|....
451 * |----- st_len -----|
453 * How many pages it should read in such pattern
454 * |-------------------------------------------------------------|
456 * |<------ length ------->|
458 * = |<----->| + |-------------------------------------| + |---|
459 * start_left st_pgs * i end_left
462 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
463 unsigned long off, unsigned long length)
465 __u64 start = off > st_off ? off - st_off : 0;
466 __u64 end = off + length > st_off ? off + length - st_off : 0;
467 unsigned long start_left = 0;
468 unsigned long end_left = 0;
469 unsigned long pg_count;
471 if (st_len == 0 || length == 0 || end == 0)
474 start_left = do_div(start, st_len);
475 if (start_left < st_pgs)
476 start_left = st_pgs - start_left;
480 end_left = do_div(end, st_len);
481 if (end_left > st_pgs)
484 CDEBUG(D_READA, "start "LPU64", end "LPU64" start_left %lu end_left %lu \n",
485 start, end, start_left, end_left);
488 pg_count = end_left - (st_pgs - start_left);
490 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
492 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
493 "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
498 static int ria_page_count(struct ra_io_arg *ria)
500 __u64 length = ria->ria_end >= ria->ria_start ?
501 ria->ria_end - ria->ria_start + 1 : 0;
503 return stride_pg_count(ria->ria_stoff, ria->ria_length,
504 ria->ria_pages, ria->ria_start,
508 /*Check whether the index is in the defined ra-window */
509 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
511 /* If ria_length == ria_pages, it means non-stride I/O mode,
512 * idx should always inside read-ahead window in this case
513 * For stride I/O mode, just check whether the idx is inside
515 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
516 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
517 ria->ria_length < ria->ria_pages);
520 static int ll_read_ahead_pages(const struct lu_env *env,
521 struct cl_io *io, struct cl_page_list *queue,
522 struct ra_io_arg *ria,
523 unsigned long *reserved_pages,
524 unsigned long *ra_end)
529 pgoff_t max_index = 0;
531 LASSERT(ria != NULL);
534 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
535 for (page_idx = ria->ria_start;
536 page_idx <= ria->ria_end && *reserved_pages > 0; page_idx++) {
537 if (ras_inside_ra_window(page_idx, ria)) {
538 /* If the page is inside the read-ahead window*/
539 rc = ll_read_ahead_page(env, io, queue,
540 page_idx, &max_index);
544 } else if (rc == -ENOLCK)
546 } else if (stride_ria) {
547 /* If it is not in the read-ahead window, and it is
548 * read-ahead mode, then check whether it should skip
551 /* FIXME: This assertion only is valid when it is for
552 * forward read-ahead, it will be fixed when backward
553 * read-ahead is implemented */
554 LASSERTF(page_idx > ria->ria_stoff, "Invalid page_idx %lu"
555 "rs %lu re %lu ro %lu rl %lu rp %lu\n", page_idx,
556 ria->ria_start, ria->ria_end, ria->ria_stoff,
557 ria->ria_length, ria->ria_pages);
558 offset = page_idx - ria->ria_stoff;
559 offset = offset % (ria->ria_length);
560 if (offset > ria->ria_pages) {
561 page_idx += ria->ria_length - offset;
562 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
563 ria->ria_length - offset);
572 int ll_readahead(const struct lu_env *env, struct cl_io *io,
573 struct cl_page_list *queue, struct ll_readahead_state *ras,
576 struct vvp_io *vio = vvp_env_io(env);
577 struct vvp_thread_info *vti = vvp_env_info(env);
578 struct cl_attr *attr = ccc_env_thread_attr(env);
579 unsigned long start = 0, end = 0, reserved;
580 unsigned long ra_end, len, mlen = 0;
582 struct ll_ra_read *bead;
583 struct ra_io_arg *ria = &vti->vti_ria;
584 struct cl_object *clob;
590 inode = ccc_object_inode(clob);
592 memset(ria, 0, sizeof *ria);
594 cl_object_attr_lock(clob);
595 ret = cl_object_attr_get(env, clob, attr);
596 cl_object_attr_unlock(clob);
602 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
606 spin_lock(&ras->ras_lock);
607 if (vio->cui_ra_window_set)
608 bead = &vio->cui_bead;
612 /* Enlarge the RA window to encompass the full read */
613 if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
614 bead->lrr_start + bead->lrr_count) {
615 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
616 ras->ras_window_start;
618 /* Reserve a part of the read-ahead window that we'll be issuing */
619 if (ras->ras_window_len) {
620 start = ras->ras_next_readahead;
621 end = ras->ras_window_start + ras->ras_window_len - 1;
624 unsigned long rpc_boundary;
626 * Align RA window to an optimal boundary.
628 * XXX This would be better to align to cl_max_pages_per_rpc
629 * instead of PTLRPC_MAX_BRW_PAGES, because the RPC size may
630 * be aligned to the RAID stripe size in the future and that
631 * is more important than the RPC size.
633 /* Note: we only trim the RPC, instead of extending the RPC
634 * to the boundary, so to avoid reading too much pages during
636 rpc_boundary = ((end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1)));
637 if (rpc_boundary > 0)
640 if (rpc_boundary > start)
643 /* Truncate RA window to end of file */
644 end = min(end, (unsigned long)((kms - 1) >> PAGE_CACHE_SHIFT));
646 ras->ras_next_readahead = max(end, end + 1);
649 ria->ria_start = start;
651 /* If stride I/O mode is detected, get stride window*/
652 if (stride_io_mode(ras)) {
653 ria->ria_stoff = ras->ras_stride_offset;
654 ria->ria_length = ras->ras_stride_length;
655 ria->ria_pages = ras->ras_stride_pages;
657 spin_unlock(&ras->ras_lock);
660 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
663 len = ria_page_count(ria);
665 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
669 CDEBUG(D_READA, DFID": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
670 PFID(lu_object_fid(&clob->co_lu)),
671 ria->ria_start, ria->ria_end,
672 bead == NULL ? 0 : bead->lrr_start,
673 bead == NULL ? 0 : bead->lrr_count,
676 /* at least to extend the readahead window to cover current read */
677 if (!hit && bead != NULL &&
678 bead->lrr_start + bead->lrr_count > ria->ria_start) {
679 /* to the end of current read window. */
680 mlen = bead->lrr_start + bead->lrr_count - ria->ria_start;
681 /* trim to RPC boundary */
682 start = ria->ria_start & (PTLRPC_MAX_BRW_PAGES - 1);
683 mlen = min(mlen, PTLRPC_MAX_BRW_PAGES - start);
686 reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len, mlen);
688 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
690 CDEBUG(D_READA, "reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
692 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
693 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
695 ret = ll_read_ahead_pages(env, io, queue, ria, &reserved, &ra_end);
698 ll_ra_count_put(ll_i2sbi(inode), reserved);
700 if (ra_end == end + 1 && ra_end == (kms >> PAGE_CACHE_SHIFT))
701 ll_ra_stats_inc(inode, RA_STAT_EOF);
703 /* if we didn't get to the end of the region we reserved from
704 * the ras we need to go back and update the ras so that the
705 * next read-ahead tries from where we left off. we only do so
706 * if the region we failed to issue read-ahead on is still ahead
707 * of the app and behind the next index to start read-ahead from */
708 CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
709 ra_end, end, ria->ria_end);
711 if (ra_end != end + 1) {
712 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
713 spin_lock(&ras->ras_lock);
714 if (ra_end < ras->ras_next_readahead &&
715 index_in_window(ra_end, ras->ras_window_start, 0,
716 ras->ras_window_len)) {
717 ras->ras_next_readahead = ra_end;
720 spin_unlock(&ras->ras_lock);
726 static void ras_set_start(struct inode *inode, struct ll_readahead_state *ras,
729 ras->ras_window_start = index & (~(RAS_INCREASE_STEP(inode) - 1));
732 /* called with the ras_lock held or from places where it doesn't matter */
733 static void ras_reset(struct inode *inode, struct ll_readahead_state *ras,
736 ras->ras_last_readpage = index;
737 ras->ras_consecutive_requests = 0;
738 ras->ras_consecutive_pages = 0;
739 ras->ras_window_len = 0;
740 ras_set_start(inode, ras, index);
741 ras->ras_next_readahead = max(ras->ras_window_start, index);
746 /* called with the ras_lock held or from places where it doesn't matter */
747 static void ras_stride_reset(struct ll_readahead_state *ras)
749 ras->ras_consecutive_stride_requests = 0;
750 ras->ras_stride_length = 0;
751 ras->ras_stride_pages = 0;
755 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
757 spin_lock_init(&ras->ras_lock);
758 ras_reset(inode, ras, 0);
759 ras->ras_requests = 0;
760 INIT_LIST_HEAD(&ras->ras_read_beads);
764 * Check whether the read request is in the stride window.
765 * If it is in the stride window, return 1, otherwise return 0.
767 static int index_in_stride_window(struct ll_readahead_state *ras,
770 unsigned long stride_gap;
772 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
773 ras->ras_stride_pages == ras->ras_stride_length)
776 stride_gap = index - ras->ras_last_readpage - 1;
778 /* If it is contiguous read */
780 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
782 /* Otherwise check the stride by itself */
783 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
784 ras->ras_consecutive_pages == ras->ras_stride_pages;
787 static void ras_update_stride_detector(struct ll_readahead_state *ras,
790 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
792 if (!stride_io_mode(ras) && (stride_gap != 0 ||
793 ras->ras_consecutive_stride_requests == 0)) {
794 ras->ras_stride_pages = ras->ras_consecutive_pages;
795 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
797 LASSERT(ras->ras_request_index == 0);
798 LASSERT(ras->ras_consecutive_stride_requests == 0);
800 if (index <= ras->ras_last_readpage) {
801 /*Reset stride window for forward read*/
802 ras_stride_reset(ras);
806 ras->ras_stride_pages = ras->ras_consecutive_pages;
807 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
814 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
816 return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
817 ras->ras_stride_pages, ras->ras_stride_offset,
821 /* Stride Read-ahead window will be increased inc_len according to
822 * stride I/O pattern */
823 static void ras_stride_increase_window(struct ll_readahead_state *ras,
824 struct ll_ra_info *ra,
825 unsigned long inc_len)
827 unsigned long left, step, window_len;
828 unsigned long stride_len;
830 LASSERT(ras->ras_stride_length > 0);
831 LASSERTF(ras->ras_window_start + ras->ras_window_len
832 >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
833 " stride_offset %lu\n", ras->ras_window_start,
834 ras->ras_window_len, ras->ras_stride_offset);
836 stride_len = ras->ras_window_start + ras->ras_window_len -
837 ras->ras_stride_offset;
839 left = stride_len % ras->ras_stride_length;
840 window_len = ras->ras_window_len - left;
842 if (left < ras->ras_stride_pages)
845 left = ras->ras_stride_pages + inc_len;
847 LASSERT(ras->ras_stride_pages != 0);
849 step = left / ras->ras_stride_pages;
850 left %= ras->ras_stride_pages;
852 window_len += step * ras->ras_stride_length + left;
854 if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
855 ras->ras_window_len = window_len;
860 static void ras_increase_window(struct inode *inode,
861 struct ll_readahead_state *ras,
862 struct ll_ra_info *ra)
864 /* The stretch of ra-window should be aligned with max rpc_size
865 * but current clio architecture does not support retrieve such
866 * information from lower layer. FIXME later
868 if (stride_io_mode(ras))
869 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP(inode));
871 ras->ras_window_len = min(ras->ras_window_len +
872 RAS_INCREASE_STEP(inode),
873 ra->ra_max_pages_per_file);
876 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
877 struct ll_readahead_state *ras, unsigned long index,
880 struct ll_ra_info *ra = &sbi->ll_ra_info;
881 int zero = 0, stride_detect = 0, ra_miss = 0;
884 spin_lock(&ras->ras_lock);
886 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
888 /* reset the read-ahead window in two cases. First when the app seeks
889 * or reads to some other part of the file. Secondly if we get a
890 * read-ahead miss that we think we've previously issued. This can
891 * be a symptom of there being so many read-ahead pages that the VM is
892 * reclaiming it before we get to it. */
893 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
895 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
896 } else if (!hit && ras->ras_window_len &&
897 index < ras->ras_next_readahead &&
898 index_in_window(index, ras->ras_window_start, 0,
899 ras->ras_window_len)) {
901 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
904 /* On the second access to a file smaller than the tunable
905 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
906 * file up to ra_max_pages_per_file. This is simply a best effort
907 * and only occurs once per open file. Normal RA behavior is reverted
908 * to for subsequent IO. The mmap case does not increment
909 * ras_requests and thus can never trigger this behavior. */
910 if (ras->ras_requests == 2 && !ras->ras_request_index) {
913 kms_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
916 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
917 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
920 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
921 ras->ras_window_start = 0;
922 ras->ras_last_readpage = 0;
923 ras->ras_next_readahead = 0;
924 ras->ras_window_len = min(ra->ra_max_pages_per_file,
925 ra->ra_max_read_ahead_whole_pages);
930 /* check whether it is in stride I/O mode*/
931 if (!index_in_stride_window(ras, index)) {
932 if (ras->ras_consecutive_stride_requests == 0 &&
933 ras->ras_request_index == 0) {
934 ras_update_stride_detector(ras, index);
935 ras->ras_consecutive_stride_requests++;
937 ras_stride_reset(ras);
939 ras_reset(inode, ras, index);
940 ras->ras_consecutive_pages++;
943 ras->ras_consecutive_pages = 0;
944 ras->ras_consecutive_requests = 0;
945 if (++ras->ras_consecutive_stride_requests > 1)
951 if (index_in_stride_window(ras, index) &&
952 stride_io_mode(ras)) {
953 /*If stride-RA hit cache miss, the stride dector
954 *will not be reset to avoid the overhead of
955 *redetecting read-ahead mode */
956 if (index != ras->ras_last_readpage + 1)
957 ras->ras_consecutive_pages = 0;
958 ras_reset(inode, ras, index);
961 /* Reset both stride window and normal RA
963 ras_reset(inode, ras, index);
964 ras->ras_consecutive_pages++;
965 ras_stride_reset(ras);
968 } else if (stride_io_mode(ras)) {
969 /* If this is contiguous read but in stride I/O mode
970 * currently, check whether stride step still is valid,
971 * if invalid, it will reset the stride ra window*/
972 if (!index_in_stride_window(ras, index)) {
973 /* Shrink stride read-ahead window to be zero */
974 ras_stride_reset(ras);
975 ras->ras_window_len = 0;
976 ras->ras_next_readahead = index;
980 ras->ras_consecutive_pages++;
981 ras->ras_last_readpage = index;
982 ras_set_start(inode, ras, index);
984 if (stride_io_mode(ras)) {
985 /* Since stride readahead is sentivite to the offset
986 * of read-ahead, so we use original offset here,
987 * instead of ras_window_start, which is RPC aligned */
988 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
990 if (ras->ras_next_readahead < ras->ras_window_start)
991 ras->ras_next_readahead = ras->ras_window_start;
993 ras->ras_next_readahead = index + 1;
997 /* Trigger RA in the mmap case where ras_consecutive_requests
998 * is not incremented and thus can't be used to trigger RA */
999 if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
1000 ras->ras_window_len = RAS_INCREASE_STEP(inode);
1001 GOTO(out_unlock, 0);
1004 /* Initially reset the stride window offset to next_readahead*/
1005 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
1007 * Once stride IO mode is detected, next_readahead should be
1008 * reset to make sure next_readahead > stride offset
1010 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
1011 ras->ras_stride_offset = index;
1012 ras->ras_window_len = RAS_INCREASE_STEP(inode);
1015 /* The initial ras_window_len is set to the request size. To avoid
1016 * uselessly reading and discarding pages for random IO the window is
1017 * only increased once per consecutive request received. */
1018 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
1019 !ras->ras_request_index)
1020 ras_increase_window(inode, ras, ra);
1024 ras->ras_request_index++;
1025 spin_unlock(&ras->ras_lock);
1029 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1031 struct inode *inode = vmpage->mapping->host;
1032 struct ll_inode_info *lli = ll_i2info(inode);
1035 struct cl_page *page;
1036 struct cl_object *clob;
1037 struct cl_env_nest nest;
1038 bool redirtied = false;
1039 bool unlocked = false;
1043 LASSERT(PageLocked(vmpage));
1044 LASSERT(!PageWriteback(vmpage));
1046 LASSERT(ll_i2dtexp(inode) != NULL);
1048 env = cl_env_nested_get(&nest);
1050 GOTO(out, result = PTR_ERR(env));
1052 clob = ll_i2info(inode)->lli_clob;
1053 LASSERT(clob != NULL);
1055 io = ccc_env_thread_io(env);
1057 io->ci_ignore_layout = 1;
1058 result = cl_io_init(env, io, CIT_MISC, clob);
1060 page = cl_page_find(env, clob, vmpage->index,
1061 vmpage, CPT_CACHEABLE);
1062 if (!IS_ERR(page)) {
1063 lu_ref_add(&page->cp_reference, "writepage",
1065 cl_page_assume(env, io, page);
1066 result = cl_page_flush(env, io, page);
1069 * Re-dirty page on error so it retries write,
1070 * but not in case when IO has actually
1071 * occurred and completed with an error.
1073 if (!PageError(vmpage)) {
1074 redirty_page_for_writepage(wbc, vmpage);
1079 cl_page_disown(env, io, page);
1081 lu_ref_del(&page->cp_reference,
1082 "writepage", current);
1083 cl_page_put(env, page);
1085 result = PTR_ERR(page);
1088 cl_io_fini(env, io);
1090 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1091 loff_t offset = cl_offset(clob, vmpage->index);
1093 /* Flush page failed because the extent is being written out.
1094 * Wait for the write of extent to be finished to avoid
1095 * breaking kernel which assumes ->writepage should mark
1096 * PageWriteback or clean the page. */
1097 result = cl_sync_file_range(inode, offset,
1098 offset + PAGE_CACHE_SIZE - 1,
1101 /* actually we may have written more than one page.
1102 * decreasing this page because the caller will count
1104 wbc->nr_to_write -= result - 1;
1109 cl_env_nested_put(&nest, env);
1114 if (!lli->lli_async_rc)
1115 lli->lli_async_rc = result;
1116 SetPageError(vmpage);
1118 unlock_page(vmpage);
1123 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1125 struct inode *inode = mapping->host;
1126 struct ll_sb_info *sbi = ll_i2sbi(inode);
1129 enum cl_fsync_mode mode;
1130 int range_whole = 0;
1132 int ignore_layout = 0;
1135 if (wbc->range_cyclic) {
1136 start = mapping->writeback_index << PAGE_CACHE_SHIFT;
1137 end = OBD_OBJECT_EOF;
1139 start = wbc->range_start;
1140 end = wbc->range_end;
1141 if (end == LLONG_MAX) {
1142 end = OBD_OBJECT_EOF;
1143 range_whole = start == 0;
1147 mode = CL_FSYNC_NONE;
1148 if (wbc->sync_mode == WB_SYNC_ALL)
1149 mode = CL_FSYNC_LOCAL;
1151 if (sbi->ll_umounting)
1152 /* if the mountpoint is being umounted, all pages have to be
1153 * evicted to avoid hitting LBUG when truncate_inode_pages()
1154 * is called later on. */
1157 if (cl_i2info(inode)->lli_clob == NULL)
1160 result = cl_sync_file_range(inode, start, end, mode, ignore_layout);
1162 wbc->nr_to_write -= result;
1166 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1167 if (end == OBD_OBJECT_EOF)
1168 mapping->writeback_index = 0;
1170 mapping->writeback_index = (end >> PAGE_CACHE_SHIFT) +1;
1175 int ll_readpage(struct file *file, struct page *vmpage)
1177 struct ll_cl_context *lcc;
1181 lcc = ll_cl_init(file, vmpage);
1183 struct lu_env *env = lcc->lcc_env;
1184 struct cl_io *io = lcc->lcc_io;
1185 struct cl_page *page = lcc->lcc_page;
1187 LASSERT(page->cp_type == CPT_CACHEABLE);
1188 if (likely(!PageUptodate(vmpage))) {
1189 cl_page_assume(env, io, page);
1190 result = cl_io_read_page(env, io, page);
1192 /* Page from a non-object file. */
1193 unlock_page(vmpage);
1198 unlock_page(vmpage);
1199 result = PTR_ERR(lcc);
1204 int ll_page_sync_io(const struct lu_env *env, struct cl_io *io,
1205 struct cl_page *page, enum cl_req_type crt)
1207 struct cl_2queue *queue;
1210 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
1212 queue = &io->ci_queue;
1213 cl_2queue_init_page(queue, page);
1215 result = cl_io_submit_sync(env, io, crt, queue, 0);
1216 LASSERT(cl_page_is_owned(page, io));
1218 if (crt == CRT_READ)
1220 * in CRT_WRITE case page is left locked even in case of
1223 cl_page_list_disown(env, io, &queue->c2_qin);
1224 cl_2queue_fini(env, queue);