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>
65 struct obd_capa *cl_capa_lookup(struct inode *inode, enum cl_req_type crt)
69 opc = crt == CRT_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
70 return ll_osscapa_get(inode, opc);
73 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
76 * Get readahead pages from the filesystem readahead pool of the client for a
79 * /param sbi superblock for filesystem readahead state ll_ra_info
80 * /param ria per-thread readahead state
81 * /param pages number of pages requested for readahead for the thread.
83 * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
84 * It should work well if the ra_max_pages is much greater than the single
85 * file's read-ahead window, and not too many threads contending for
86 * these readahead pages.
88 * TODO: There may be a 'global sync problem' if many threads are trying
89 * to get an ra budget that is larger than the remaining readahead pages
90 * and reach here at exactly the same time. They will compute /a ret to
91 * consume the remaining pages, but will fail at atomic_add_return() and
92 * get a zero ra window, although there is still ra space remaining. - Jay */
94 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
95 struct ra_io_arg *ria,
96 unsigned long pages, unsigned long min)
98 struct ll_ra_info *ra = &sbi->ll_ra_info;
102 /* If read-ahead pages left are less than 1M, do not do read-ahead,
103 * otherwise it will form small read RPC(< 1M), which hurt server
104 * performance a lot. */
105 ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages), pages);
106 if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages))
109 /* If the non-strided (ria_pages == 0) readahead window
110 * (ria_start + ret) has grown across an RPC boundary, then trim
111 * readahead size by the amount beyond the RPC so it ends on an
112 * RPC boundary. If the readahead window is already ending on
113 * an RPC boundary (beyond_rpc == 0), or smaller than a full
114 * RPC (beyond_rpc < ret) the readahead size is unchanged.
115 * The (beyond_rpc != 0) check is skipped since the conditional
116 * branch is more expensive than subtracting zero from the result.
118 * Strided read is left unaligned to avoid small fragments beyond
119 * the RPC boundary from needing an extra read RPC. */
120 if (ria->ria_pages == 0) {
121 long beyond_rpc = (ria->ria_start + ret) % PTLRPC_MAX_BRW_PAGES;
122 if (/* beyond_rpc != 0 && */ beyond_rpc < ret)
126 if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
127 atomic_sub(ret, &ra->ra_cur_pages);
133 /* override ra limit for maximum performance */
134 atomic_add(min - ret, &ra->ra_cur_pages);
140 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
142 struct ll_ra_info *ra = &sbi->ll_ra_info;
143 atomic_sub(len, &ra->ra_cur_pages);
146 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
148 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
149 lprocfs_counter_incr(sbi->ll_ra_stats, which);
152 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
154 struct ll_sb_info *sbi = ll_i2sbi(inode);
155 ll_ra_stats_inc_sbi(sbi, which);
158 #define RAS_CDEBUG(ras) \
160 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu" \
161 "csr %lu sf %lu sp %lu sl %lu \n", \
162 ras->ras_last_readpage, ras->ras_consecutive_requests, \
163 ras->ras_consecutive_pages, ras->ras_window_start, \
164 ras->ras_window_len, ras->ras_next_readahead, \
165 ras->ras_requests, ras->ras_request_index, \
166 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
167 ras->ras_stride_pages, ras->ras_stride_length)
169 static int index_in_window(unsigned long index, unsigned long point,
170 unsigned long before, unsigned long after)
172 unsigned long start = point - before, end = point + after;
179 return start <= index && index <= end;
182 static struct ll_readahead_state *ll_ras_get(struct file *f)
184 struct ll_file_data *fd;
186 fd = LUSTRE_FPRIVATE(f);
190 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar)
192 struct ll_readahead_state *ras;
196 spin_lock(&ras->ras_lock);
198 ras->ras_request_index = 0;
199 ras->ras_consecutive_requests++;
200 rar->lrr_reader = current;
202 list_add(&rar->lrr_linkage, &ras->ras_read_beads);
203 spin_unlock(&ras->ras_lock);
206 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
208 struct ll_readahead_state *ras;
212 spin_lock(&ras->ras_lock);
213 list_del_init(&rar->lrr_linkage);
214 spin_unlock(&ras->ras_lock);
217 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
219 struct ll_ra_read *scan;
221 list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
222 if (scan->lrr_reader == current)
228 struct ll_ra_read *ll_ra_read_get(struct file *f)
230 struct ll_readahead_state *ras;
231 struct ll_ra_read *bead;
235 spin_lock(&ras->ras_lock);
236 bead = ll_ra_read_get_locked(ras);
237 spin_unlock(&ras->ras_lock);
241 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
242 struct cl_page_list *queue, struct cl_page *page,
243 struct cl_object *clob, pgoff_t *max_index)
245 struct page *vmpage = page->cp_vmpage;
252 cl_page_assume(env, io, page);
253 lu_ref_add(&page->cp_reference, "ra", current);
254 cp = cl2ccc_page(cl_object_page_slice(clob, page));
255 if (!cp->cpg_defer_uptodate && !PageUptodate(vmpage)) {
256 CDEBUG(D_READA, "page index %lu, max_index: %lu\n",
257 ccc_index(cp), *max_index);
258 /* Disable the optimization on prefetching maximum readahead
259 * index because there is a race with lock cancellation. This
260 * optimization will be revived later.
261 * if (*max_index == 0 || ccc_index(cp) > *max_index) */
262 rc = cl_page_is_under_lock(env, io, page, max_index);
264 cp->cpg_defer_uptodate = 1;
266 cl_page_list_add(queue, page);
269 cl_page_discard(env, io, page);
273 /* skip completed pages */
274 cl_page_unassume(env, io, page);
276 lu_ref_del(&page->cp_reference, "ra", current);
277 cl_page_put(env, page);
282 * Initiates read-ahead of a page with given index.
284 * \retval +ve: page was added to \a queue.
286 * \retval -ENOLCK: there is no extent lock for this part of a file, stop
289 * \retval -ve, 0: page wasn't added to \a queue for other reason.
291 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
292 struct cl_page_list *queue,
293 pgoff_t index, pgoff_t *max_index)
295 struct cl_object *clob = io->ci_obj;
296 struct inode *inode = ccc_object_inode(clob);
298 struct cl_page *page;
299 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
300 unsigned int gfp_mask;
302 const char *msg = NULL;
306 gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
308 gfp_mask |= __GFP_NOWARN;
310 vmpage = grab_cache_page_nowait(inode->i_mapping, index);
311 if (vmpage != NULL) {
312 /* Check if vmpage was truncated or reclaimed */
313 if (vmpage->mapping == inode->i_mapping) {
314 page = cl_page_find(env, clob, vmpage->index,
315 vmpage, CPT_CACHEABLE);
317 rc = cl_read_ahead_page(env, io, queue,
318 page, clob, max_index);
320 which = RA_STAT_FAILED_MATCH;
321 msg = "lock match failed";
324 which = RA_STAT_FAILED_GRAB_PAGE;
325 msg = "cl_page_find failed";
328 which = RA_STAT_WRONG_GRAB_PAGE;
329 msg = "g_c_p_n returned invalid page";
333 page_cache_release(vmpage);
335 which = RA_STAT_FAILED_GRAB_PAGE;
336 msg = "g_c_p_n failed";
339 ll_ra_stats_inc(inode, which);
340 CDEBUG(D_READA, "%s\n", msg);
345 #define RIA_DEBUG(ria) \
346 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
347 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
350 /* Limit this to the blocksize instead of PTLRPC_BRW_MAX_SIZE, since we don't
351 * know what the actual RPC size is. If this needs to change, it makes more
352 * sense to tune the i_blkbits value for the file based on the OSTs it is
353 * striped over, rather than having a constant value for all files here. */
355 /* RAS_INCREASE_STEP should be (1UL << (inode->i_blkbits - PAGE_CACHE_SHIFT)).
356 * Temprarily set RAS_INCREASE_STEP to 1MB. After 4MB RPC is enabled
357 * by default, this should be adjusted corresponding with max_read_ahead_mb
358 * and max_read_ahead_per_file_mb otherwise the readahead budget can be used
359 * up quickly which will affect read performance siginificantly. See LU-2816 */
360 #define RAS_INCREASE_STEP(inode) (ONE_MB_BRW_SIZE >> PAGE_CACHE_SHIFT)
362 static inline int stride_io_mode(struct ll_readahead_state *ras)
364 return ras->ras_consecutive_stride_requests > 1;
366 /* The function calculates how much pages will be read in
367 * [off, off + length], in such stride IO area,
368 * stride_offset = st_off, stride_lengh = st_len,
369 * stride_pages = st_pgs
371 * |------------------|*****|------------------|*****|------------|*****|....
374 * |----- st_len -----|
376 * How many pages it should read in such pattern
377 * |-------------------------------------------------------------|
379 * |<------ length ------->|
381 * = |<----->| + |-------------------------------------| + |---|
382 * start_left st_pgs * i end_left
385 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
386 unsigned long off, unsigned long length)
388 __u64 start = off > st_off ? off - st_off : 0;
389 __u64 end = off + length > st_off ? off + length - st_off : 0;
390 unsigned long start_left = 0;
391 unsigned long end_left = 0;
392 unsigned long pg_count;
394 if (st_len == 0 || length == 0 || end == 0)
397 start_left = do_div(start, st_len);
398 if (start_left < st_pgs)
399 start_left = st_pgs - start_left;
403 end_left = do_div(end, st_len);
404 if (end_left > st_pgs)
407 CDEBUG(D_READA, "start "LPU64", end "LPU64" start_left %lu end_left %lu \n",
408 start, end, start_left, end_left);
411 pg_count = end_left - (st_pgs - start_left);
413 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
415 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
416 "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
421 static int ria_page_count(struct ra_io_arg *ria)
423 __u64 length = ria->ria_end >= ria->ria_start ?
424 ria->ria_end - ria->ria_start + 1 : 0;
426 return stride_pg_count(ria->ria_stoff, ria->ria_length,
427 ria->ria_pages, ria->ria_start,
431 /*Check whether the index is in the defined ra-window */
432 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
434 /* If ria_length == ria_pages, it means non-stride I/O mode,
435 * idx should always inside read-ahead window in this case
436 * For stride I/O mode, just check whether the idx is inside
438 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
439 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
440 ria->ria_length < ria->ria_pages);
443 static int ll_read_ahead_pages(const struct lu_env *env,
444 struct cl_io *io, struct cl_page_list *queue,
445 struct ra_io_arg *ria,
446 unsigned long *reserved_pages,
447 unsigned long *ra_end)
452 pgoff_t max_index = 0;
454 LASSERT(ria != NULL);
457 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
458 for (page_idx = ria->ria_start;
459 page_idx <= ria->ria_end && *reserved_pages > 0; page_idx++) {
460 if (ras_inside_ra_window(page_idx, ria)) {
461 /* If the page is inside the read-ahead window*/
462 rc = ll_read_ahead_page(env, io, queue,
463 page_idx, &max_index);
467 } else if (rc == -ENOLCK)
469 } else if (stride_ria) {
470 /* If it is not in the read-ahead window, and it is
471 * read-ahead mode, then check whether it should skip
474 /* FIXME: This assertion only is valid when it is for
475 * forward read-ahead, it will be fixed when backward
476 * read-ahead is implemented */
477 LASSERTF(page_idx > ria->ria_stoff, "Invalid page_idx %lu"
478 "rs %lu re %lu ro %lu rl %lu rp %lu\n", page_idx,
479 ria->ria_start, ria->ria_end, ria->ria_stoff,
480 ria->ria_length, ria->ria_pages);
481 offset = page_idx - ria->ria_stoff;
482 offset = offset % (ria->ria_length);
483 if (offset > ria->ria_pages) {
484 page_idx += ria->ria_length - offset;
485 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
486 ria->ria_length - offset);
495 int ll_readahead(const struct lu_env *env, struct cl_io *io,
496 struct cl_page_list *queue, struct ll_readahead_state *ras,
499 struct vvp_io *vio = vvp_env_io(env);
500 struct vvp_thread_info *vti = vvp_env_info(env);
501 struct cl_attr *attr = ccc_env_thread_attr(env);
502 unsigned long start = 0, end = 0, reserved;
503 unsigned long ra_end, len, mlen = 0;
505 struct ll_ra_read *bead;
506 struct ra_io_arg *ria = &vti->vti_ria;
507 struct cl_object *clob;
513 inode = ccc_object_inode(clob);
515 memset(ria, 0, sizeof *ria);
517 cl_object_attr_lock(clob);
518 ret = cl_object_attr_get(env, clob, attr);
519 cl_object_attr_unlock(clob);
525 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
529 spin_lock(&ras->ras_lock);
530 if (vio->cui_ra_window_set)
531 bead = &vio->cui_bead;
535 /* Enlarge the RA window to encompass the full read */
536 if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
537 bead->lrr_start + bead->lrr_count) {
538 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
539 ras->ras_window_start;
541 /* Reserve a part of the read-ahead window that we'll be issuing */
542 if (ras->ras_window_len) {
543 start = ras->ras_next_readahead;
544 end = ras->ras_window_start + ras->ras_window_len - 1;
547 unsigned long rpc_boundary;
549 * Align RA window to an optimal boundary.
551 * XXX This would be better to align to cl_max_pages_per_rpc
552 * instead of PTLRPC_MAX_BRW_PAGES, because the RPC size may
553 * be aligned to the RAID stripe size in the future and that
554 * is more important than the RPC size.
556 /* Note: we only trim the RPC, instead of extending the RPC
557 * to the boundary, so to avoid reading too much pages during
559 rpc_boundary = ((end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1)));
560 if (rpc_boundary > 0)
563 if (rpc_boundary > start)
566 /* Truncate RA window to end of file */
567 end = min(end, (unsigned long)((kms - 1) >> PAGE_CACHE_SHIFT));
569 ras->ras_next_readahead = max(end, end + 1);
572 ria->ria_start = start;
574 /* If stride I/O mode is detected, get stride window*/
575 if (stride_io_mode(ras)) {
576 ria->ria_stoff = ras->ras_stride_offset;
577 ria->ria_length = ras->ras_stride_length;
578 ria->ria_pages = ras->ras_stride_pages;
580 spin_unlock(&ras->ras_lock);
583 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
586 len = ria_page_count(ria);
588 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
592 CDEBUG(D_READA, DFID": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
593 PFID(lu_object_fid(&clob->co_lu)),
594 ria->ria_start, ria->ria_end,
595 bead == NULL ? 0 : bead->lrr_start,
596 bead == NULL ? 0 : bead->lrr_count,
599 /* at least to extend the readahead window to cover current read */
600 if (!hit && bead != NULL &&
601 bead->lrr_start + bead->lrr_count > ria->ria_start) {
602 /* to the end of current read window. */
603 mlen = bead->lrr_start + bead->lrr_count - ria->ria_start;
604 /* trim to RPC boundary */
605 start = ria->ria_start & (PTLRPC_MAX_BRW_PAGES - 1);
606 mlen = min(mlen, PTLRPC_MAX_BRW_PAGES - start);
609 reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len, mlen);
611 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
613 CDEBUG(D_READA, "reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
615 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
616 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
618 ret = ll_read_ahead_pages(env, io, queue, ria, &reserved, &ra_end);
621 ll_ra_count_put(ll_i2sbi(inode), reserved);
623 if (ra_end == end + 1 && ra_end == (kms >> PAGE_CACHE_SHIFT))
624 ll_ra_stats_inc(inode, RA_STAT_EOF);
626 /* if we didn't get to the end of the region we reserved from
627 * the ras we need to go back and update the ras so that the
628 * next read-ahead tries from where we left off. we only do so
629 * if the region we failed to issue read-ahead on is still ahead
630 * of the app and behind the next index to start read-ahead from */
631 CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
632 ra_end, end, ria->ria_end);
634 if (ra_end != end + 1) {
635 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
636 spin_lock(&ras->ras_lock);
637 if (ra_end < ras->ras_next_readahead &&
638 index_in_window(ra_end, ras->ras_window_start, 0,
639 ras->ras_window_len)) {
640 ras->ras_next_readahead = ra_end;
643 spin_unlock(&ras->ras_lock);
649 static void ras_set_start(struct inode *inode, struct ll_readahead_state *ras,
652 ras->ras_window_start = index & (~(RAS_INCREASE_STEP(inode) - 1));
655 /* called with the ras_lock held or from places where it doesn't matter */
656 static void ras_reset(struct inode *inode, struct ll_readahead_state *ras,
659 ras->ras_last_readpage = index;
660 ras->ras_consecutive_requests = 0;
661 ras->ras_consecutive_pages = 0;
662 ras->ras_window_len = 0;
663 ras_set_start(inode, ras, index);
664 ras->ras_next_readahead = max(ras->ras_window_start, index);
669 /* called with the ras_lock held or from places where it doesn't matter */
670 static void ras_stride_reset(struct ll_readahead_state *ras)
672 ras->ras_consecutive_stride_requests = 0;
673 ras->ras_stride_length = 0;
674 ras->ras_stride_pages = 0;
678 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
680 spin_lock_init(&ras->ras_lock);
681 ras_reset(inode, ras, 0);
682 ras->ras_requests = 0;
683 INIT_LIST_HEAD(&ras->ras_read_beads);
687 * Check whether the read request is in the stride window.
688 * If it is in the stride window, return 1, otherwise return 0.
690 static int index_in_stride_window(struct ll_readahead_state *ras,
693 unsigned long stride_gap;
695 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
696 ras->ras_stride_pages == ras->ras_stride_length)
699 stride_gap = index - ras->ras_last_readpage - 1;
701 /* If it is contiguous read */
703 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
705 /* Otherwise check the stride by itself */
706 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
707 ras->ras_consecutive_pages == ras->ras_stride_pages;
710 static void ras_update_stride_detector(struct ll_readahead_state *ras,
713 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
715 if (!stride_io_mode(ras) && (stride_gap != 0 ||
716 ras->ras_consecutive_stride_requests == 0)) {
717 ras->ras_stride_pages = ras->ras_consecutive_pages;
718 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
720 LASSERT(ras->ras_request_index == 0);
721 LASSERT(ras->ras_consecutive_stride_requests == 0);
723 if (index <= ras->ras_last_readpage) {
724 /*Reset stride window for forward read*/
725 ras_stride_reset(ras);
729 ras->ras_stride_pages = ras->ras_consecutive_pages;
730 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
737 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
739 return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
740 ras->ras_stride_pages, ras->ras_stride_offset,
744 /* Stride Read-ahead window will be increased inc_len according to
745 * stride I/O pattern */
746 static void ras_stride_increase_window(struct ll_readahead_state *ras,
747 struct ll_ra_info *ra,
748 unsigned long inc_len)
750 unsigned long left, step, window_len;
751 unsigned long stride_len;
753 LASSERT(ras->ras_stride_length > 0);
754 LASSERTF(ras->ras_window_start + ras->ras_window_len
755 >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
756 " stride_offset %lu\n", ras->ras_window_start,
757 ras->ras_window_len, ras->ras_stride_offset);
759 stride_len = ras->ras_window_start + ras->ras_window_len -
760 ras->ras_stride_offset;
762 left = stride_len % ras->ras_stride_length;
763 window_len = ras->ras_window_len - left;
765 if (left < ras->ras_stride_pages)
768 left = ras->ras_stride_pages + inc_len;
770 LASSERT(ras->ras_stride_pages != 0);
772 step = left / ras->ras_stride_pages;
773 left %= ras->ras_stride_pages;
775 window_len += step * ras->ras_stride_length + left;
777 if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
778 ras->ras_window_len = window_len;
783 static void ras_increase_window(struct inode *inode,
784 struct ll_readahead_state *ras,
785 struct ll_ra_info *ra)
787 /* The stretch of ra-window should be aligned with max rpc_size
788 * but current clio architecture does not support retrieve such
789 * information from lower layer. FIXME later
791 if (stride_io_mode(ras))
792 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP(inode));
794 ras->ras_window_len = min(ras->ras_window_len +
795 RAS_INCREASE_STEP(inode),
796 ra->ra_max_pages_per_file);
799 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
800 struct ll_readahead_state *ras, unsigned long index,
803 struct ll_ra_info *ra = &sbi->ll_ra_info;
804 int zero = 0, stride_detect = 0, ra_miss = 0;
807 spin_lock(&ras->ras_lock);
809 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
811 /* reset the read-ahead window in two cases. First when the app seeks
812 * or reads to some other part of the file. Secondly if we get a
813 * read-ahead miss that we think we've previously issued. This can
814 * be a symptom of there being so many read-ahead pages that the VM is
815 * reclaiming it before we get to it. */
816 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
818 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
819 } else if (!hit && ras->ras_window_len &&
820 index < ras->ras_next_readahead &&
821 index_in_window(index, ras->ras_window_start, 0,
822 ras->ras_window_len)) {
824 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
827 /* On the second access to a file smaller than the tunable
828 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
829 * file up to ra_max_pages_per_file. This is simply a best effort
830 * and only occurs once per open file. Normal RA behavior is reverted
831 * to for subsequent IO. The mmap case does not increment
832 * ras_requests and thus can never trigger this behavior. */
833 if (ras->ras_requests == 2 && !ras->ras_request_index) {
836 kms_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
839 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
840 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
843 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
844 ras->ras_window_start = 0;
845 ras->ras_last_readpage = 0;
846 ras->ras_next_readahead = 0;
847 ras->ras_window_len = min(ra->ra_max_pages_per_file,
848 ra->ra_max_read_ahead_whole_pages);
853 /* check whether it is in stride I/O mode*/
854 if (!index_in_stride_window(ras, index)) {
855 if (ras->ras_consecutive_stride_requests == 0 &&
856 ras->ras_request_index == 0) {
857 ras_update_stride_detector(ras, index);
858 ras->ras_consecutive_stride_requests++;
860 ras_stride_reset(ras);
862 ras_reset(inode, ras, index);
863 ras->ras_consecutive_pages++;
866 ras->ras_consecutive_pages = 0;
867 ras->ras_consecutive_requests = 0;
868 if (++ras->ras_consecutive_stride_requests > 1)
874 if (index_in_stride_window(ras, index) &&
875 stride_io_mode(ras)) {
876 /*If stride-RA hit cache miss, the stride dector
877 *will not be reset to avoid the overhead of
878 *redetecting read-ahead mode */
879 if (index != ras->ras_last_readpage + 1)
880 ras->ras_consecutive_pages = 0;
881 ras_reset(inode, ras, index);
884 /* Reset both stride window and normal RA
886 ras_reset(inode, ras, index);
887 ras->ras_consecutive_pages++;
888 ras_stride_reset(ras);
891 } else if (stride_io_mode(ras)) {
892 /* If this is contiguous read but in stride I/O mode
893 * currently, check whether stride step still is valid,
894 * if invalid, it will reset the stride ra window*/
895 if (!index_in_stride_window(ras, index)) {
896 /* Shrink stride read-ahead window to be zero */
897 ras_stride_reset(ras);
898 ras->ras_window_len = 0;
899 ras->ras_next_readahead = index;
903 ras->ras_consecutive_pages++;
904 ras->ras_last_readpage = index;
905 ras_set_start(inode, ras, index);
907 if (stride_io_mode(ras)) {
908 /* Since stride readahead is sentivite to the offset
909 * of read-ahead, so we use original offset here,
910 * instead of ras_window_start, which is RPC aligned */
911 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
913 if (ras->ras_next_readahead < ras->ras_window_start)
914 ras->ras_next_readahead = ras->ras_window_start;
916 ras->ras_next_readahead = index + 1;
920 /* Trigger RA in the mmap case where ras_consecutive_requests
921 * is not incremented and thus can't be used to trigger RA */
922 if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
923 ras->ras_window_len = RAS_INCREASE_STEP(inode);
927 /* Initially reset the stride window offset to next_readahead*/
928 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
930 * Once stride IO mode is detected, next_readahead should be
931 * reset to make sure next_readahead > stride offset
933 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
934 ras->ras_stride_offset = index;
935 ras->ras_window_len = RAS_INCREASE_STEP(inode);
938 /* The initial ras_window_len is set to the request size. To avoid
939 * uselessly reading and discarding pages for random IO the window is
940 * only increased once per consecutive request received. */
941 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
942 !ras->ras_request_index)
943 ras_increase_window(inode, ras, ra);
947 ras->ras_request_index++;
948 spin_unlock(&ras->ras_lock);
952 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
954 struct inode *inode = vmpage->mapping->host;
955 struct ll_inode_info *lli = ll_i2info(inode);
958 struct cl_page *page;
959 struct cl_object *clob;
960 struct cl_env_nest nest;
961 bool redirtied = false;
962 bool unlocked = false;
966 LASSERT(PageLocked(vmpage));
967 LASSERT(!PageWriteback(vmpage));
969 LASSERT(ll_i2dtexp(inode) != NULL);
971 env = cl_env_nested_get(&nest);
973 GOTO(out, result = PTR_ERR(env));
975 clob = ll_i2info(inode)->lli_clob;
976 LASSERT(clob != NULL);
978 io = ccc_env_thread_io(env);
980 io->ci_ignore_layout = 1;
981 result = cl_io_init(env, io, CIT_MISC, clob);
983 page = cl_page_find(env, clob, vmpage->index,
984 vmpage, CPT_CACHEABLE);
986 lu_ref_add(&page->cp_reference, "writepage",
988 cl_page_assume(env, io, page);
989 result = cl_page_flush(env, io, page);
992 * Re-dirty page on error so it retries write,
993 * but not in case when IO has actually
994 * occurred and completed with an error.
996 if (!PageError(vmpage)) {
997 redirty_page_for_writepage(wbc, vmpage);
1002 cl_page_disown(env, io, page);
1004 lu_ref_del(&page->cp_reference,
1005 "writepage", current);
1006 cl_page_put(env, page);
1008 result = PTR_ERR(page);
1011 cl_io_fini(env, io);
1013 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1014 loff_t offset = cl_offset(clob, vmpage->index);
1016 /* Flush page failed because the extent is being written out.
1017 * Wait for the write of extent to be finished to avoid
1018 * breaking kernel which assumes ->writepage should mark
1019 * PageWriteback or clean the page. */
1020 result = cl_sync_file_range(inode, offset,
1021 offset + PAGE_CACHE_SIZE - 1,
1024 /* actually we may have written more than one page.
1025 * decreasing this page because the caller will count
1027 wbc->nr_to_write -= result - 1;
1032 cl_env_nested_put(&nest, env);
1037 if (!lli->lli_async_rc)
1038 lli->lli_async_rc = result;
1039 SetPageError(vmpage);
1041 unlock_page(vmpage);
1046 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1048 struct inode *inode = mapping->host;
1049 struct ll_sb_info *sbi = ll_i2sbi(inode);
1052 enum cl_fsync_mode mode;
1053 int range_whole = 0;
1055 int ignore_layout = 0;
1058 if (wbc->range_cyclic) {
1059 start = mapping->writeback_index << PAGE_CACHE_SHIFT;
1060 end = OBD_OBJECT_EOF;
1062 start = wbc->range_start;
1063 end = wbc->range_end;
1064 if (end == LLONG_MAX) {
1065 end = OBD_OBJECT_EOF;
1066 range_whole = start == 0;
1070 mode = CL_FSYNC_NONE;
1071 if (wbc->sync_mode == WB_SYNC_ALL)
1072 mode = CL_FSYNC_LOCAL;
1074 if (sbi->ll_umounting)
1075 /* if the mountpoint is being umounted, all pages have to be
1076 * evicted to avoid hitting LBUG when truncate_inode_pages()
1077 * is called later on. */
1080 if (cl_i2info(inode)->lli_clob == NULL)
1083 result = cl_sync_file_range(inode, start, end, mode, ignore_layout);
1085 wbc->nr_to_write -= result;
1089 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1090 if (end == OBD_OBJECT_EOF)
1091 mapping->writeback_index = 0;
1093 mapping->writeback_index = (end >> PAGE_CACHE_SHIFT) +1;
1098 struct ll_cl_context *ll_cl_find(struct file *file)
1100 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1101 struct ll_cl_context *lcc;
1102 struct ll_cl_context *found = NULL;
1104 read_lock(&fd->fd_lock);
1105 list_for_each_entry(lcc, &fd->fd_lccs, lcc_list) {
1106 if (lcc->lcc_cookie == current) {
1111 read_unlock(&fd->fd_lock);
1116 void ll_cl_add(struct file *file, const struct lu_env *env, struct cl_io *io)
1118 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1119 struct ll_cl_context *lcc = &vvp_env_info(env)->vti_io_ctx;
1121 memset(lcc, 0, sizeof(*lcc));
1122 INIT_LIST_HEAD(&lcc->lcc_list);
1123 lcc->lcc_cookie = current;
1127 write_lock(&fd->fd_lock);
1128 list_add(&lcc->lcc_list, &fd->fd_lccs);
1129 write_unlock(&fd->fd_lock);
1132 void ll_cl_remove(struct file *file, const struct lu_env *env)
1134 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1135 struct ll_cl_context *lcc = &vvp_env_info(env)->vti_io_ctx;
1137 write_lock(&fd->fd_lock);
1138 list_del_init(&lcc->lcc_list);
1139 write_unlock(&fd->fd_lock);
1142 int ll_readpage(struct file *file, struct page *vmpage)
1144 struct cl_object *clob = ll_i2info(file->f_dentry->d_inode)->lli_clob;
1145 struct ll_cl_context *lcc;
1146 const struct lu_env *env;
1148 struct cl_page *page;
1152 lcc = ll_cl_find(file);
1154 unlock_page(vmpage);
1160 LASSERT(io != NULL);
1161 LASSERT(io->ci_state == CIS_IO_GOING);
1162 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
1163 if (!IS_ERR(page)) {
1164 LASSERT(page->cp_type == CPT_CACHEABLE);
1165 if (likely(!PageUptodate(vmpage))) {
1166 cl_page_assume(env, io, page);
1167 result = cl_io_read_page(env, io, page);
1169 /* Page from a non-object file. */
1170 unlock_page(vmpage);
1173 cl_page_put(env, page);
1175 unlock_page(vmpage);
1176 result = PTR_ERR(page);
1181 int ll_page_sync_io(const struct lu_env *env, struct cl_io *io,
1182 struct cl_page *page, enum cl_req_type crt)
1184 struct cl_2queue *queue;
1187 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
1189 queue = &io->ci_queue;
1190 cl_2queue_init_page(queue, page);
1192 result = cl_io_submit_sync(env, io, crt, queue, 0);
1193 LASSERT(cl_page_is_owned(page, io));
1195 if (crt == CRT_READ)
1197 * in CRT_WRITE case page is left locked even in case of
1200 cl_page_list_disown(env, io, &queue->c2_qin);
1201 cl_2queue_fini(env, queue);