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 if (io && lcc->lcc_created) {
85 cl_io_unlock(env, io);
86 cl_io_iter_fini(env, io);
89 cl_env_put(env, &lcc->lcc_refcheck);
93 * Initializes common cl-data at the typical address_space operation entry
96 struct ll_cl_context *ll_cl_init(struct file *file, struct page *vmpage)
98 struct ll_cl_context *lcc;
101 struct cl_object *clob;
107 clob = ll_i2info(file->f_dentry->d_inode)->lli_clob;
108 LASSERT(clob != NULL);
110 env = cl_env_get(&refcheck);
112 return ERR_PTR(PTR_ERR(env));
114 lcc = &vvp_env_info(env)->vti_io_ctx;
115 memset(lcc, 0, sizeof(*lcc));
117 lcc->lcc_refcheck = refcheck;
118 lcc->lcc_cookie = current;
120 cio = ccc_env_io(env);
121 io = cio->cui_cl.cis_io;
124 struct inode *inode = file->f_dentry->d_inode;
126 CERROR("%s: " DFID " no active IO, please file a ticket.\n",
127 ll_get_fsname(inode->i_sb, NULL, 0),
128 PFID(ll_inode2fid(inode)));
129 libcfs_debug_dumpstack(NULL);
132 if (result == 0 && vmpage != NULL) {
133 struct cl_page *page;
136 LASSERT(io->ci_state == CIS_IO_GOING);
137 LASSERT(cio->cui_fd == LUSTRE_FPRIVATE(file));
138 page = cl_page_find(env, clob, vmpage->index, vmpage,
141 lcc->lcc_page = page;
142 lu_ref_add(&page->cp_reference, "cl_io", io);
145 result = PTR_ERR(page);
149 lcc = ERR_PTR(result);
155 struct obd_capa *cl_capa_lookup(struct inode *inode, enum cl_req_type crt)
159 opc = crt == CRT_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
160 return ll_osscapa_get(inode, opc);
163 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
166 * Get readahead pages from the filesystem readahead pool of the client for a
169 * /param sbi superblock for filesystem readahead state ll_ra_info
170 * /param ria per-thread readahead state
171 * /param pages number of pages requested for readahead for the thread.
173 * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
174 * It should work well if the ra_max_pages is much greater than the single
175 * file's read-ahead window, and not too many threads contending for
176 * these readahead pages.
178 * TODO: There may be a 'global sync problem' if many threads are trying
179 * to get an ra budget that is larger than the remaining readahead pages
180 * and reach here at exactly the same time. They will compute /a ret to
181 * consume the remaining pages, but will fail at atomic_add_return() and
182 * get a zero ra window, although there is still ra space remaining. - Jay */
184 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
185 struct ra_io_arg *ria,
188 struct ll_ra_info *ra = &sbi->ll_ra_info;
192 /* If read-ahead pages left are less than 1M, do not do read-ahead,
193 * otherwise it will form small read RPC(< 1M), which hurt server
194 * performance a lot. */
195 ret = min(ra->ra_max_pages - cfs_atomic_read(&ra->ra_cur_pages), pages);
196 if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages))
199 /* If the non-strided (ria_pages == 0) readahead window
200 * (ria_start + ret) has grown across an RPC boundary, then trim
201 * readahead size by the amount beyond the RPC so it ends on an
202 * RPC boundary. If the readahead window is already ending on
203 * an RPC boundary (beyond_rpc == 0), or smaller than a full
204 * RPC (beyond_rpc < ret) the readahead size is unchanged.
205 * The (beyond_rpc != 0) check is skipped since the conditional
206 * branch is more expensive than subtracting zero from the result.
208 * Strided read is left unaligned to avoid small fragments beyond
209 * the RPC boundary from needing an extra read RPC. */
210 if (ria->ria_pages == 0) {
211 long beyond_rpc = (ria->ria_start + ret) % PTLRPC_MAX_BRW_PAGES;
212 if (/* beyond_rpc != 0 && */ beyond_rpc < ret)
216 if (cfs_atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
217 cfs_atomic_sub(ret, &ra->ra_cur_pages);
225 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
227 struct ll_ra_info *ra = &sbi->ll_ra_info;
228 cfs_atomic_sub(len, &ra->ra_cur_pages);
231 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
233 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
234 lprocfs_counter_incr(sbi->ll_ra_stats, which);
237 void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
239 struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
240 ll_ra_stats_inc_sbi(sbi, which);
243 #define RAS_CDEBUG(ras) \
245 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu" \
246 "csr %lu sf %lu sp %lu sl %lu \n", \
247 ras->ras_last_readpage, ras->ras_consecutive_requests, \
248 ras->ras_consecutive_pages, ras->ras_window_start, \
249 ras->ras_window_len, ras->ras_next_readahead, \
250 ras->ras_requests, ras->ras_request_index, \
251 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
252 ras->ras_stride_pages, ras->ras_stride_length)
254 static int index_in_window(unsigned long index, unsigned long point,
255 unsigned long before, unsigned long after)
257 unsigned long start = point - before, end = point + after;
264 return start <= index && index <= end;
267 static struct ll_readahead_state *ll_ras_get(struct file *f)
269 struct ll_file_data *fd;
271 fd = LUSTRE_FPRIVATE(f);
275 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar)
277 struct ll_readahead_state *ras;
281 spin_lock(&ras->ras_lock);
283 ras->ras_request_index = 0;
284 ras->ras_consecutive_requests++;
285 rar->lrr_reader = current;
287 cfs_list_add(&rar->lrr_linkage, &ras->ras_read_beads);
288 spin_unlock(&ras->ras_lock);
291 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
293 struct ll_readahead_state *ras;
297 spin_lock(&ras->ras_lock);
298 cfs_list_del_init(&rar->lrr_linkage);
299 spin_unlock(&ras->ras_lock);
302 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
304 struct ll_ra_read *scan;
306 cfs_list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
307 if (scan->lrr_reader == current)
313 struct ll_ra_read *ll_ra_read_get(struct file *f)
315 struct ll_readahead_state *ras;
316 struct ll_ra_read *bead;
320 spin_lock(&ras->ras_lock);
321 bead = ll_ra_read_get_locked(ras);
322 spin_unlock(&ras->ras_lock);
326 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
327 struct cl_page_list *queue, struct cl_page *page,
328 struct cl_object *clob)
330 struct page *vmpage = page->cp_vmpage;
337 cl_page_assume(env, io, page);
338 lu_ref_add(&page->cp_reference, "ra", current);
339 cp = cl2ccc_page(cl_object_page_slice(clob, page));
340 if (!cp->cpg_defer_uptodate && !PageUptodate(vmpage)) {
341 rc = cl_page_is_under_lock(env, io, page);
343 cp->cpg_defer_uptodate = 1;
345 cl_page_list_add(queue, page);
348 cl_page_discard(env, io, page);
352 /* skip completed pages */
353 cl_page_unassume(env, io, page);
355 lu_ref_del(&page->cp_reference, "ra", current);
356 cl_page_put(env, page);
361 * Initiates read-ahead of a page with given index.
363 * \retval +ve: page was added to \a queue.
365 * \retval -ENOLCK: there is no extent lock for this part of a file, stop
368 * \retval -ve, 0: page wasn't added to \a queue for other reason.
370 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
371 struct cl_page_list *queue,
372 pgoff_t index, struct address_space *mapping)
375 struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
376 struct cl_page *page;
377 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
378 unsigned int gfp_mask;
380 const char *msg = NULL;
384 gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
386 gfp_mask |= __GFP_NOWARN;
388 vmpage = grab_cache_page_nowait(mapping, index);
389 if (vmpage != NULL) {
390 /* Check if vmpage was truncated or reclaimed */
391 if (vmpage->mapping == mapping) {
392 page = cl_page_find(env, clob, vmpage->index,
393 vmpage, CPT_CACHEABLE);
395 rc = cl_read_ahead_page(env, io, queue,
398 which = RA_STAT_FAILED_MATCH;
399 msg = "lock match failed";
402 which = RA_STAT_FAILED_GRAB_PAGE;
403 msg = "cl_page_find failed";
406 which = RA_STAT_WRONG_GRAB_PAGE;
407 msg = "g_c_p_n returned invalid page";
411 page_cache_release(vmpage);
413 which = RA_STAT_FAILED_GRAB_PAGE;
414 msg = "g_c_p_n failed";
417 ll_ra_stats_inc(mapping, which);
418 CDEBUG(D_READA, "%s\n", msg);
423 #define RIA_DEBUG(ria) \
424 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
425 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
428 /* Limit this to the blocksize instead of PTLRPC_BRW_MAX_SIZE, since we don't
429 * know what the actual RPC size is. If this needs to change, it makes more
430 * sense to tune the i_blkbits value for the file based on the OSTs it is
431 * striped over, rather than having a constant value for all files here. */
433 /* RAS_INCREASE_STEP should be (1UL << (inode->i_blkbits - PAGE_CACHE_SHIFT)).
434 * Temprarily set RAS_INCREASE_STEP to 1MB. After 4MB RPC is enabled
435 * by default, this should be adjusted corresponding with max_read_ahead_mb
436 * and max_read_ahead_per_file_mb otherwise the readahead budget can be used
437 * up quickly which will affect read performance siginificantly. See LU-2816 */
438 #define RAS_INCREASE_STEP(inode) (ONE_MB_BRW_SIZE >> PAGE_CACHE_SHIFT)
440 static inline int stride_io_mode(struct ll_readahead_state *ras)
442 return ras->ras_consecutive_stride_requests > 1;
444 /* The function calculates how much pages will be read in
445 * [off, off + length], in such stride IO area,
446 * stride_offset = st_off, stride_lengh = st_len,
447 * stride_pages = st_pgs
449 * |------------------|*****|------------------|*****|------------|*****|....
452 * |----- st_len -----|
454 * How many pages it should read in such pattern
455 * |-------------------------------------------------------------|
457 * |<------ length ------->|
459 * = |<----->| + |-------------------------------------| + |---|
460 * start_left st_pgs * i end_left
463 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
464 unsigned long off, unsigned long length)
466 __u64 start = off > st_off ? off - st_off : 0;
467 __u64 end = off + length > st_off ? off + length - st_off : 0;
468 unsigned long start_left = 0;
469 unsigned long end_left = 0;
470 unsigned long pg_count;
472 if (st_len == 0 || length == 0 || end == 0)
475 start_left = do_div(start, st_len);
476 if (start_left < st_pgs)
477 start_left = st_pgs - start_left;
481 end_left = do_div(end, st_len);
482 if (end_left > st_pgs)
485 CDEBUG(D_READA, "start "LPU64", end "LPU64" start_left %lu end_left %lu \n",
486 start, end, start_left, end_left);
489 pg_count = end_left - (st_pgs - start_left);
491 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
493 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
494 "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
499 static int ria_page_count(struct ra_io_arg *ria)
501 __u64 length = ria->ria_end >= ria->ria_start ?
502 ria->ria_end - ria->ria_start + 1 : 0;
504 return stride_pg_count(ria->ria_stoff, ria->ria_length,
505 ria->ria_pages, ria->ria_start,
509 /*Check whether the index is in the defined ra-window */
510 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
512 /* If ria_length == ria_pages, it means non-stride I/O mode,
513 * idx should always inside read-ahead window in this case
514 * For stride I/O mode, just check whether the idx is inside
516 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
517 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
518 ria->ria_length < ria->ria_pages);
521 static int ll_read_ahead_pages(const struct lu_env *env,
522 struct cl_io *io, struct cl_page_list *queue,
523 struct ra_io_arg *ria,
524 unsigned long *reserved_pages,
525 struct address_space *mapping,
526 unsigned long *ra_end)
528 int rc, count = 0, stride_ria;
529 unsigned long page_idx;
531 LASSERT(ria != NULL);
534 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
535 for (page_idx = ria->ria_start; page_idx <= ria->ria_end &&
536 *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,
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 ll_readahead_state *ras, struct address_space *mapping,
574 struct cl_page_list *queue, int flags)
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;
582 struct ll_ra_read *bead;
583 struct ra_io_arg *ria = &vti->vti_ria;
584 struct ll_inode_info *lli;
585 struct cl_object *clob;
590 inode = mapping->host;
591 lli = ll_i2info(inode);
592 clob = lli->lli_clob;
594 memset(ria, 0, sizeof *ria);
596 cl_object_attr_lock(clob);
597 ret = cl_object_attr_get(env, clob, attr);
598 cl_object_attr_unlock(clob);
604 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
608 spin_lock(&ras->ras_lock);
609 if (vio->cui_ra_window_set)
610 bead = &vio->cui_bead;
614 /* Enlarge the RA window to encompass the full read */
615 if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
616 bead->lrr_start + bead->lrr_count) {
617 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
618 ras->ras_window_start;
620 /* Reserve a part of the read-ahead window that we'll be issuing */
621 if (ras->ras_window_len) {
622 start = ras->ras_next_readahead;
623 end = ras->ras_window_start + ras->ras_window_len - 1;
626 unsigned long rpc_boundary;
628 * Align RA window to an optimal boundary.
630 * XXX This would be better to align to cl_max_pages_per_rpc
631 * instead of PTLRPC_MAX_BRW_PAGES, because the RPC size may
632 * be aligned to the RAID stripe size in the future and that
633 * is more important than the RPC size.
635 /* Note: we only trim the RPC, instead of extending the RPC
636 * to the boundary, so to avoid reading too much pages during
638 rpc_boundary = ((end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1)));
639 if (rpc_boundary > 0)
642 if (rpc_boundary > start)
645 /* Truncate RA window to end of file */
646 end = min(end, (unsigned long)((kms - 1) >> PAGE_CACHE_SHIFT));
648 ras->ras_next_readahead = max(end, end + 1);
651 ria->ria_start = start;
653 /* If stride I/O mode is detected, get stride window*/
654 if (stride_io_mode(ras)) {
655 ria->ria_stoff = ras->ras_stride_offset;
656 ria->ria_length = ras->ras_stride_length;
657 ria->ria_pages = ras->ras_stride_pages;
659 spin_unlock(&ras->ras_lock);
662 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
665 len = ria_page_count(ria);
669 reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len);
671 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
673 CDEBUG(D_READA, "reserved page %lu ra_cur %d ra_max %lu\n", reserved,
674 cfs_atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
675 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
677 ret = ll_read_ahead_pages(env, io, queue,
678 ria, &reserved, mapping, &ra_end);
681 ll_ra_count_put(ll_i2sbi(inode), reserved);
683 if (ra_end == end + 1 && ra_end == (kms >> PAGE_CACHE_SHIFT))
684 ll_ra_stats_inc(mapping, RA_STAT_EOF);
686 /* if we didn't get to the end of the region we reserved from
687 * the ras we need to go back and update the ras so that the
688 * next read-ahead tries from where we left off. we only do so
689 * if the region we failed to issue read-ahead on is still ahead
690 * of the app and behind the next index to start read-ahead from */
691 CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
692 ra_end, end, ria->ria_end);
694 if (ra_end != end + 1) {
695 spin_lock(&ras->ras_lock);
696 if (ra_end < ras->ras_next_readahead &&
697 index_in_window(ra_end, ras->ras_window_start, 0,
698 ras->ras_window_len)) {
699 ras->ras_next_readahead = ra_end;
702 spin_unlock(&ras->ras_lock);
708 static void ras_set_start(struct inode *inode, struct ll_readahead_state *ras,
711 ras->ras_window_start = index & (~(RAS_INCREASE_STEP(inode) - 1));
714 /* called with the ras_lock held or from places where it doesn't matter */
715 static void ras_reset(struct inode *inode, struct ll_readahead_state *ras,
718 ras->ras_last_readpage = index;
719 ras->ras_consecutive_requests = 0;
720 ras->ras_consecutive_pages = 0;
721 ras->ras_window_len = 0;
722 ras_set_start(inode, ras, index);
723 ras->ras_next_readahead = max(ras->ras_window_start, index);
728 /* called with the ras_lock held or from places where it doesn't matter */
729 static void ras_stride_reset(struct ll_readahead_state *ras)
731 ras->ras_consecutive_stride_requests = 0;
732 ras->ras_stride_length = 0;
733 ras->ras_stride_pages = 0;
737 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
739 spin_lock_init(&ras->ras_lock);
740 ras_reset(inode, ras, 0);
741 ras->ras_requests = 0;
742 CFS_INIT_LIST_HEAD(&ras->ras_read_beads);
746 * Check whether the read request is in the stride window.
747 * If it is in the stride window, return 1, otherwise return 0.
749 static int index_in_stride_window(struct ll_readahead_state *ras,
752 unsigned long stride_gap;
754 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
755 ras->ras_stride_pages == ras->ras_stride_length)
758 stride_gap = index - ras->ras_last_readpage - 1;
760 /* If it is contiguous read */
762 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
764 /* Otherwise check the stride by itself */
765 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
766 ras->ras_consecutive_pages == ras->ras_stride_pages;
769 static void ras_update_stride_detector(struct ll_readahead_state *ras,
772 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
774 if (!stride_io_mode(ras) && (stride_gap != 0 ||
775 ras->ras_consecutive_stride_requests == 0)) {
776 ras->ras_stride_pages = ras->ras_consecutive_pages;
777 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
779 LASSERT(ras->ras_request_index == 0);
780 LASSERT(ras->ras_consecutive_stride_requests == 0);
782 if (index <= ras->ras_last_readpage) {
783 /*Reset stride window for forward read*/
784 ras_stride_reset(ras);
788 ras->ras_stride_pages = ras->ras_consecutive_pages;
789 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
796 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
798 return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
799 ras->ras_stride_pages, ras->ras_stride_offset,
803 /* Stride Read-ahead window will be increased inc_len according to
804 * stride I/O pattern */
805 static void ras_stride_increase_window(struct ll_readahead_state *ras,
806 struct ll_ra_info *ra,
807 unsigned long inc_len)
809 unsigned long left, step, window_len;
810 unsigned long stride_len;
812 LASSERT(ras->ras_stride_length > 0);
813 LASSERTF(ras->ras_window_start + ras->ras_window_len
814 >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
815 " stride_offset %lu\n", ras->ras_window_start,
816 ras->ras_window_len, ras->ras_stride_offset);
818 stride_len = ras->ras_window_start + ras->ras_window_len -
819 ras->ras_stride_offset;
821 left = stride_len % ras->ras_stride_length;
822 window_len = ras->ras_window_len - left;
824 if (left < ras->ras_stride_pages)
827 left = ras->ras_stride_pages + inc_len;
829 LASSERT(ras->ras_stride_pages != 0);
831 step = left / ras->ras_stride_pages;
832 left %= ras->ras_stride_pages;
834 window_len += step * ras->ras_stride_length + left;
836 if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
837 ras->ras_window_len = window_len;
842 static void ras_increase_window(struct inode *inode,
843 struct ll_readahead_state *ras,
844 struct ll_ra_info *ra)
846 /* The stretch of ra-window should be aligned with max rpc_size
847 * but current clio architecture does not support retrieve such
848 * information from lower layer. FIXME later
850 if (stride_io_mode(ras))
851 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP(inode));
853 ras->ras_window_len = min(ras->ras_window_len +
854 RAS_INCREASE_STEP(inode),
855 ra->ra_max_pages_per_file);
858 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
859 struct ll_readahead_state *ras, unsigned long index,
862 struct ll_ra_info *ra = &sbi->ll_ra_info;
863 int zero = 0, stride_detect = 0, ra_miss = 0;
866 spin_lock(&ras->ras_lock);
868 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
870 /* reset the read-ahead window in two cases. First when the app seeks
871 * or reads to some other part of the file. Secondly if we get a
872 * read-ahead miss that we think we've previously issued. This can
873 * be a symptom of there being so many read-ahead pages that the VM is
874 * reclaiming it before we get to it. */
875 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
877 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
878 } else if (!hit && ras->ras_window_len &&
879 index < ras->ras_next_readahead &&
880 index_in_window(index, ras->ras_window_start, 0,
881 ras->ras_window_len)) {
883 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
886 /* On the second access to a file smaller than the tunable
887 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
888 * file up to ra_max_pages_per_file. This is simply a best effort
889 * and only occurs once per open file. Normal RA behavior is reverted
890 * to for subsequent IO. The mmap case does not increment
891 * ras_requests and thus can never trigger this behavior. */
892 if (ras->ras_requests == 2 && !ras->ras_request_index) {
895 kms_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
898 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
899 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
902 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
903 ras->ras_window_start = 0;
904 ras->ras_last_readpage = 0;
905 ras->ras_next_readahead = 0;
906 ras->ras_window_len = min(ra->ra_max_pages_per_file,
907 ra->ra_max_read_ahead_whole_pages);
912 /* check whether it is in stride I/O mode*/
913 if (!index_in_stride_window(ras, index)) {
914 if (ras->ras_consecutive_stride_requests == 0 &&
915 ras->ras_request_index == 0) {
916 ras_update_stride_detector(ras, index);
917 ras->ras_consecutive_stride_requests++;
919 ras_stride_reset(ras);
921 ras_reset(inode, ras, index);
922 ras->ras_consecutive_pages++;
925 ras->ras_consecutive_pages = 0;
926 ras->ras_consecutive_requests = 0;
927 if (++ras->ras_consecutive_stride_requests > 1)
933 if (index_in_stride_window(ras, index) &&
934 stride_io_mode(ras)) {
935 /*If stride-RA hit cache miss, the stride dector
936 *will not be reset to avoid the overhead of
937 *redetecting read-ahead mode */
938 if (index != ras->ras_last_readpage + 1)
939 ras->ras_consecutive_pages = 0;
940 ras_reset(inode, ras, index);
943 /* Reset both stride window and normal RA
945 ras_reset(inode, ras, index);
946 ras->ras_consecutive_pages++;
947 ras_stride_reset(ras);
950 } else if (stride_io_mode(ras)) {
951 /* If this is contiguous read but in stride I/O mode
952 * currently, check whether stride step still is valid,
953 * if invalid, it will reset the stride ra window*/
954 if (!index_in_stride_window(ras, index)) {
955 /* Shrink stride read-ahead window to be zero */
956 ras_stride_reset(ras);
957 ras->ras_window_len = 0;
958 ras->ras_next_readahead = index;
962 ras->ras_consecutive_pages++;
963 ras->ras_last_readpage = index;
964 ras_set_start(inode, ras, index);
966 if (stride_io_mode(ras))
967 /* Since stride readahead is sentivite to the offset
968 * of read-ahead, so we use original offset here,
969 * instead of ras_window_start, which is RPC aligned */
970 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
972 ras->ras_next_readahead = max(ras->ras_window_start,
973 ras->ras_next_readahead);
976 /* Trigger RA in the mmap case where ras_consecutive_requests
977 * is not incremented and thus can't be used to trigger RA */
978 if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
979 ras->ras_window_len = RAS_INCREASE_STEP(inode);
983 /* Initially reset the stride window offset to next_readahead*/
984 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
986 * Once stride IO mode is detected, next_readahead should be
987 * reset to make sure next_readahead > stride offset
989 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
990 ras->ras_stride_offset = index;
991 ras->ras_window_len = RAS_INCREASE_STEP(inode);
994 /* The initial ras_window_len is set to the request size. To avoid
995 * uselessly reading and discarding pages for random IO the window is
996 * only increased once per consecutive request received. */
997 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
998 !ras->ras_request_index)
999 ras_increase_window(inode, ras, ra);
1003 ras->ras_request_index++;
1004 spin_unlock(&ras->ras_lock);
1008 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1010 struct inode *inode = vmpage->mapping->host;
1011 struct ll_inode_info *lli = ll_i2info(inode);
1014 struct cl_page *page;
1015 struct cl_object *clob;
1016 struct cl_env_nest nest;
1017 bool redirtied = false;
1018 bool unlocked = false;
1022 LASSERT(PageLocked(vmpage));
1023 LASSERT(!PageWriteback(vmpage));
1025 LASSERT(ll_i2dtexp(inode) != NULL);
1027 env = cl_env_nested_get(&nest);
1029 GOTO(out, result = PTR_ERR(env));
1031 clob = ll_i2info(inode)->lli_clob;
1032 LASSERT(clob != NULL);
1034 io = ccc_env_thread_io(env);
1036 io->ci_ignore_layout = 1;
1037 result = cl_io_init(env, io, CIT_MISC, clob);
1039 page = cl_page_find(env, clob, vmpage->index,
1040 vmpage, CPT_CACHEABLE);
1041 if (!IS_ERR(page)) {
1042 lu_ref_add(&page->cp_reference, "writepage",
1044 cl_page_assume(env, io, page);
1045 result = cl_page_flush(env, io, page);
1048 * Re-dirty page on error so it retries write,
1049 * but not in case when IO has actually
1050 * occurred and completed with an error.
1052 if (!PageError(vmpage)) {
1053 redirty_page_for_writepage(wbc, vmpage);
1058 cl_page_disown(env, io, page);
1060 lu_ref_del(&page->cp_reference,
1061 "writepage", current);
1062 cl_page_put(env, page);
1064 result = PTR_ERR(page);
1067 cl_io_fini(env, io);
1069 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1070 loff_t offset = cl_offset(clob, vmpage->index);
1072 /* Flush page failed because the extent is being written out.
1073 * Wait for the write of extent to be finished to avoid
1074 * breaking kernel which assumes ->writepage should mark
1075 * PageWriteback or clean the page. */
1076 result = cl_sync_file_range(inode, offset,
1077 offset + PAGE_CACHE_SIZE - 1,
1080 /* actually we may have written more than one page.
1081 * decreasing this page because the caller will count
1083 wbc->nr_to_write -= result - 1;
1088 cl_env_nested_put(&nest, env);
1093 if (!lli->lli_async_rc)
1094 lli->lli_async_rc = result;
1095 SetPageError(vmpage);
1097 unlock_page(vmpage);
1102 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1104 struct inode *inode = mapping->host;
1105 struct ll_sb_info *sbi = ll_i2sbi(inode);
1108 enum cl_fsync_mode mode;
1109 int range_whole = 0;
1111 int ignore_layout = 0;
1114 if (wbc->range_cyclic) {
1115 start = mapping->writeback_index << PAGE_CACHE_SHIFT;
1116 end = OBD_OBJECT_EOF;
1118 start = wbc->range_start;
1119 end = wbc->range_end;
1120 if (end == LLONG_MAX) {
1121 end = OBD_OBJECT_EOF;
1122 range_whole = start == 0;
1126 mode = CL_FSYNC_NONE;
1127 if (wbc->sync_mode == WB_SYNC_ALL)
1128 mode = CL_FSYNC_LOCAL;
1130 if (sbi->ll_umounting)
1131 /* if the mountpoint is being umounted, all pages have to be
1132 * evicted to avoid hitting LBUG when truncate_inode_pages()
1133 * is called later on. */
1135 result = cl_sync_file_range(inode, start, end, mode, ignore_layout);
1137 wbc->nr_to_write -= result;
1141 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1142 if (end == OBD_OBJECT_EOF)
1143 mapping->writeback_index = 0;
1145 mapping->writeback_index = (end >> PAGE_CACHE_SHIFT) +1;
1150 int ll_readpage(struct file *file, struct page *vmpage)
1152 struct ll_cl_context *lcc;
1156 lcc = ll_cl_init(file, vmpage);
1158 struct lu_env *env = lcc->lcc_env;
1159 struct cl_io *io = lcc->lcc_io;
1160 struct cl_page *page = lcc->lcc_page;
1162 LASSERT(page->cp_type == CPT_CACHEABLE);
1163 if (likely(!PageUptodate(vmpage))) {
1164 cl_page_assume(env, io, page);
1165 result = cl_io_read_page(env, io, page);
1167 /* Page from a non-object file. */
1168 unlock_page(vmpage);
1173 unlock_page(vmpage);
1174 result = PTR_ERR(lcc);
1179 int ll_page_sync_io(const struct lu_env *env, struct cl_io *io,
1180 struct cl_page *page, enum cl_req_type crt)
1182 struct cl_2queue *queue;
1185 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
1187 queue = &io->ci_queue;
1188 cl_2queue_init_page(queue, page);
1190 result = cl_io_submit_sync(env, io, crt, queue, 0);
1191 LASSERT(cl_page_is_owned(page, io));
1193 if (crt == CRT_READ)
1195 * in CRT_WRITE case page is left locked even in case of
1198 cl_page_list_disown(env, io, &queue->c2_qin);
1199 cl_2queue_fini(env, queue);