1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
40 * Lustre Lite I/O page cache routines shared by different kernel revs
43 #include <linux/kernel.h>
45 #include <linux/string.h>
46 #include <linux/stat.h>
47 #include <linux/errno.h>
48 #include <linux/smp_lock.h>
49 #include <linux/unistd.h>
50 #include <linux/version.h>
51 #include <asm/system.h>
52 #include <asm/uaccess.h>
55 #include <linux/stat.h>
56 #include <asm/uaccess.h>
58 #include <linux/pagemap.h>
59 #include <linux/smp_lock.h>
60 /* current_is_kswapd() */
61 #include <linux/swap.h>
63 #define DEBUG_SUBSYSTEM S_LLITE
65 #include <lustre_lite.h>
66 #include <obd_cksum.h>
67 #include "llite_internal.h"
68 #include <linux/lustre_compat25.h>
70 /* this isn't where truncate starts. roughly:
71 * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate. setattr_raw grabs
72 * DLM lock on [size, EOF], i_mutex, ->lli_size_sem, and WRITE_I_ALLOC_SEM to
75 * must be called under ->lli_size_sem */
76 void ll_truncate(struct inode *inode)
80 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) to %llu\n", inode->i_ino,
81 inode->i_generation, inode, i_size_read(inode));
83 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_TRUNC, 1);
90 * Finalizes cl-data before exiting typical address_space operation. Dual to
93 static void ll_cl_fini(struct ll_cl_context *lcc)
95 struct lu_env *env = lcc->lcc_env;
96 struct cl_io *io = lcc->lcc_io;
97 struct cl_page *page = lcc->lcc_page;
99 LASSERT(lcc->lcc_cookie == current);
100 LASSERT(env != NULL);
103 lu_ref_del(&page->cp_reference, "cl_io", io);
104 cl_page_put(env, page);
107 if (io && lcc->lcc_created) {
109 cl_io_unlock(env, io);
110 cl_io_iter_fini(env, io);
113 cl_env_put(env, &lcc->lcc_refcheck);
117 * Initializes common cl-data at the typical address_space operation entry
120 static struct ll_cl_context *ll_cl_init(struct file *file,
121 struct page *vmpage, int create)
123 struct ll_cl_context *lcc;
126 struct cl_object *clob;
132 clob = ll_i2info(vmpage->mapping->host)->lli_clob;
133 LASSERT(clob != NULL);
135 env = cl_env_get(&refcheck);
137 return ERR_PTR(PTR_ERR(env));
139 lcc = &vvp_env_info(env)->vti_io_ctx;
140 memset(lcc, 0, sizeof(*lcc));
142 lcc->lcc_refcheck = refcheck;
143 lcc->lcc_cookie = current;
145 cio = ccc_env_io(env);
146 io = cio->cui_cl.cis_io;
147 if (io == NULL && create) {
151 * Loop-back driver calls ->prepare_write() and ->sendfile()
152 * methods directly, bypassing file system ->write() operation,
153 * so cl_io has to be created here.
155 io = ccc_env_thread_io(env);
156 ll_io_init(io, file, 1);
158 /* No lock at all for this kind of IO - we can't do it because
159 * we have held page lock, it would cause deadlock.
160 * XXX: This causes poor performance to loop device - One page
162 * In order to get better performance, users should use
163 * lloop driver instead.
165 io->ci_lockreq = CILR_NEVER;
167 pos = (vmpage->index << CFS_PAGE_SHIFT);
169 /* Create a temp IO to serve write. */
170 result = cl_io_rw_init(env, io, CIT_WRITE, pos, CFS_PAGE_SIZE);
172 cio->cui_fd = LUSTRE_FPRIVATE(file);
175 result = cl_io_iter_init(env, io);
177 result = cl_io_lock(env, io);
179 result = cl_io_start(env, io);
182 result = io->ci_result;
183 lcc->lcc_created = 1;
190 struct cl_page *page;
193 LASSERT(io->ci_state == CIS_IO_GOING);
194 LASSERT(cio->cui_fd == LUSTRE_FPRIVATE(file));
195 page = cl_page_find(env, clob, vmpage->index, vmpage,
198 lcc->lcc_page = page;
199 lu_ref_add(&page->cp_reference, "cl_io", io);
202 result = PTR_ERR(page);
206 lcc = ERR_PTR(result);
209 CDEBUG(D_VFSTRACE, "%lu@"DFID" -> %d %p %p\n",
210 vmpage->index, PFID(lu_object_fid(&clob->co_lu)), result,
215 static struct ll_cl_context *ll_cl_get(void)
217 struct ll_cl_context *lcc;
221 env = cl_env_get(&refcheck);
222 LASSERT(!IS_ERR(env));
223 lcc = &vvp_env_info(env)->vti_io_ctx;
224 LASSERT(env == lcc->lcc_env);
225 LASSERT(current == lcc->lcc_cookie);
226 cl_env_put(env, &refcheck);
228 /* env has got in ll_cl_init, so it is still usable. */
233 * ->prepare_write() address space operation called by generic_file_write()
234 * for every page during write.
236 int ll_prepare_write(struct file *file, struct page *vmpage, unsigned from,
239 struct ll_cl_context *lcc;
243 lcc = ll_cl_init(file, vmpage, 1);
245 struct lu_env *env = lcc->lcc_env;
246 struct cl_io *io = lcc->lcc_io;
247 struct cl_page *page = lcc->lcc_page;
249 cl_page_assume(env, io, page);
250 if (cl_io_is_append(io)) {
251 struct cl_object *obj = io->ci_obj;
252 struct inode *inode = ccc_object_inode(obj);
254 * In VFS file->page write loop, for appending, the
255 * write offset might be reset according to the new
256 * file size before holding i_mutex. So crw_pos should
257 * be reset here. BUG:17711.
259 io->u.ci_wr.wr.crw_pos = i_size_read(inode);
261 result = cl_io_prepare_write(env, io, page, from, to);
264 * Add a reference, so that page is not evicted from
265 * the cache until ->commit_write() is called.
268 lu_ref_add(&page->cp_reference, "prepare_write",
271 cl_page_unassume(env, io, page);
274 /* returning 0 in prepare assumes commit must be called
277 result = PTR_ERR(lcc);
282 int ll_commit_write(struct file *file, struct page *vmpage, unsigned from,
285 struct ll_cl_context *lcc;
288 struct cl_page *page;
294 page = lcc->lcc_page;
297 LASSERT(cl_page_is_owned(page, io));
299 if (from != to) /* handle short write case. */
300 result = cl_io_commit_write(env, io, page, from, to);
301 if (cl_page_is_owned(page, io))
302 cl_page_unassume(env, io, page);
305 * Release reference acquired by ll_prepare_write().
307 lu_ref_del(&page->cp_reference, "prepare_write", cfs_current());
308 cl_page_put(env, page);
313 struct obd_capa *cl_capa_lookup(struct inode *inode, enum cl_req_type crt)
317 opc = crt == CRT_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
318 return ll_osscapa_get(inode, opc);
321 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
323 /* WARNING: This algorithm is used to reduce the contention on
324 * sbi->ll_lock. It should work well if the ra_max_pages is much
325 * greater than the single file's read-ahead window.
327 * TODO: There may exist a `global sync problem' in this implementation.
328 * Considering the global ra window is 100M, and each file's ra window is 10M,
329 * there are over 10 files trying to get its ra budget and reach
330 * ll_ra_count_get at the exactly same time. All of them will get a zero ra
331 * window, although the global window is 100M. -jay
333 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi, struct ra_io_arg *ria,
336 struct ll_ra_info *ra = &sbi->ll_ra_info;
341 * If read-ahead pages left are less than 1M, do not do read-ahead,
342 * otherwise it will form small read RPC(< 1M), which hurt server
345 if (ra->ra_max_pages < atomic_read(&ra->ra_cur_pages))
348 ret = min(ra->ra_max_pages - cfs_atomic_read(&ra->ra_cur_pages), len);
349 if ((int)ret < 0 || ret < min((unsigned long)PTLRPC_MAX_BRW_PAGES, len))
352 if (ria->ria_pages == 0)
353 /* it needs 1M align again after trimed by ra_max_pages*/
354 if (ret >= ((ria->ria_start + ret) % PTLRPC_MAX_BRW_PAGES))
355 ret -= (ria->ria_start + ret) % PTLRPC_MAX_BRW_PAGES;
357 if (cfs_atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
358 cfs_atomic_sub(ret, &ra->ra_cur_pages);
366 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
368 struct ll_ra_info *ra = &sbi->ll_ra_info;
369 cfs_atomic_sub(len, &ra->ra_cur_pages);
372 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
374 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
375 lprocfs_counter_incr(sbi->ll_ra_stats, which);
378 void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
380 struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
381 ll_ra_stats_inc_sbi(sbi, which);
384 #define RAS_CDEBUG(ras) \
386 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu" \
387 "csr %lu sf %lu sp %lu sl %lu \n", \
388 ras->ras_last_readpage, ras->ras_consecutive_requests, \
389 ras->ras_consecutive_pages, ras->ras_window_start, \
390 ras->ras_window_len, ras->ras_next_readahead, \
391 ras->ras_requests, ras->ras_request_index, \
392 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
393 ras->ras_stride_pages, ras->ras_stride_length)
395 static int index_in_window(unsigned long index, unsigned long point,
396 unsigned long before, unsigned long after)
398 unsigned long start = point - before, end = point + after;
405 return start <= index && index <= end;
408 static struct ll_readahead_state *ll_ras_get(struct file *f)
410 struct ll_file_data *fd;
412 fd = LUSTRE_FPRIVATE(f);
416 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar)
418 struct ll_readahead_state *ras;
422 cfs_spin_lock(&ras->ras_lock);
424 ras->ras_request_index = 0;
425 ras->ras_consecutive_requests++;
426 rar->lrr_reader = current;
428 cfs_list_add(&rar->lrr_linkage, &ras->ras_read_beads);
429 cfs_spin_unlock(&ras->ras_lock);
432 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
434 struct ll_readahead_state *ras;
438 cfs_spin_lock(&ras->ras_lock);
439 cfs_list_del_init(&rar->lrr_linkage);
440 cfs_spin_unlock(&ras->ras_lock);
443 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
445 struct ll_ra_read *scan;
447 cfs_list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
448 if (scan->lrr_reader == current)
454 struct ll_ra_read *ll_ra_read_get(struct file *f)
456 struct ll_readahead_state *ras;
457 struct ll_ra_read *bead;
461 cfs_spin_lock(&ras->ras_lock);
462 bead = ll_ra_read_get_locked(ras);
463 cfs_spin_unlock(&ras->ras_lock);
467 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
468 struct cl_page_list *queue, struct cl_page *page,
477 cl_page_assume(env, io, page);
478 lu_ref_add(&page->cp_reference, "ra", cfs_current());
479 cp = cl2ccc_page(cl_page_at(page, &vvp_device_type));
480 if (!cp->cpg_defer_uptodate && !Page_Uptodate(vmpage)) {
481 rc = cl_page_is_under_lock(env, io, page);
483 cp->cpg_defer_uptodate = 1;
485 cl_page_list_add(queue, page);
488 cl_page_delete(env, page);
492 /* skip completed pages */
493 cl_page_unassume(env, io, page);
494 lu_ref_del(&page->cp_reference, "ra", cfs_current());
495 cl_page_put(env, page);
500 * Initiates read-ahead of a page with given index.
502 * \retval +ve: page was added to \a queue.
504 * \retval -ENOLCK: there is no extent lock for this part of a file, stop
507 * \retval -ve, 0: page wasn't added to \a queue for other reason.
509 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
510 struct cl_page_list *queue,
511 pgoff_t index, struct address_space *mapping)
514 struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
515 struct cl_page *page;
516 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
517 unsigned int gfp_mask;
519 const char *msg = NULL;
523 gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
525 gfp_mask |= __GFP_NOWARN;
527 vmpage = grab_cache_page_nowait_gfp(mapping, index, gfp_mask);
528 if (vmpage != NULL) {
529 /* Check if vmpage was truncated or reclaimed */
530 if (vmpage->mapping == mapping) {
531 page = cl_page_find(env, clob, vmpage->index,
532 vmpage, CPT_CACHEABLE);
534 rc = cl_read_ahead_page(env, io, queue,
537 which = RA_STAT_FAILED_MATCH;
538 msg = "lock match failed";
541 which = RA_STAT_FAILED_GRAB_PAGE;
542 msg = "cl_page_find failed";
545 which = RA_STAT_WRONG_GRAB_PAGE;
546 msg = "g_c_p_n returned invalid page";
550 page_cache_release(vmpage);
552 which = RA_STAT_FAILED_GRAB_PAGE;
553 msg = "g_c_p_n failed";
556 ll_ra_stats_inc(mapping, which);
557 CDEBUG(D_READA, "%s\n", msg);
562 #define RIA_DEBUG(ria) \
563 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
564 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
567 #define RAS_INCREASE_STEP PTLRPC_MAX_BRW_PAGES
569 static inline int stride_io_mode(struct ll_readahead_state *ras)
571 return ras->ras_consecutive_stride_requests > 1;
573 /* The function calculates how much pages will be read in
574 * [off, off + length], in such stride IO area,
575 * stride_offset = st_off, stride_lengh = st_len,
576 * stride_pages = st_pgs
578 * |------------------|*****|------------------|*****|------------|*****|....
581 * |----- st_len -----|
583 * How many pages it should read in such pattern
584 * |-------------------------------------------------------------|
586 * |<------ length ------->|
588 * = |<----->| + |-------------------------------------| + |---|
589 * start_left st_pgs * i end_left
592 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
593 unsigned long off, unsigned long length)
595 __u64 start = off > st_off ? off - st_off : 0;
596 __u64 end = off + length > st_off ? off + length - st_off : 0;
597 unsigned long start_left = 0;
598 unsigned long end_left = 0;
599 unsigned long pg_count;
601 if (st_len == 0 || length == 0 || end == 0)
604 start_left = do_div(start, st_len);
605 if (start_left < st_pgs)
606 start_left = st_pgs - start_left;
610 end_left = do_div(end, st_len);
611 if (end_left > st_pgs)
614 CDEBUG(D_READA, "start "LPU64", end "LPU64" start_left %lu end_left %lu \n",
615 start, end, start_left, end_left);
618 pg_count = end_left - (st_pgs - start_left);
620 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
622 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
623 "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
628 static int ria_page_count(struct ra_io_arg *ria)
630 __u64 length = ria->ria_end >= ria->ria_start ?
631 ria->ria_end - ria->ria_start + 1 : 0;
633 return stride_pg_count(ria->ria_stoff, ria->ria_length,
634 ria->ria_pages, ria->ria_start,
638 /*Check whether the index is in the defined ra-window */
639 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
641 /* If ria_length == ria_pages, it means non-stride I/O mode,
642 * idx should always inside read-ahead window in this case
643 * For stride I/O mode, just check whether the idx is inside
645 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
646 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
647 ria->ria_length < ria->ria_pages);
650 static int ll_read_ahead_pages(const struct lu_env *env,
651 struct cl_io *io, struct cl_page_list *queue,
652 struct ra_io_arg *ria,
653 unsigned long *reserved_pages,
654 struct address_space *mapping,
655 unsigned long *ra_end)
657 int rc, count = 0, stride_ria;
658 unsigned long page_idx;
660 LASSERT(ria != NULL);
663 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
664 for (page_idx = ria->ria_start; page_idx <= ria->ria_end &&
665 *reserved_pages > 0; page_idx++) {
666 if (ras_inside_ra_window(page_idx, ria)) {
667 /* If the page is inside the read-ahead window*/
668 rc = ll_read_ahead_page(env, io, queue,
673 } else if (rc == -ENOLCK)
675 } else if (stride_ria) {
676 /* If it is not in the read-ahead window, and it is
677 * read-ahead mode, then check whether it should skip
680 /* FIXME: This assertion only is valid when it is for
681 * forward read-ahead, it will be fixed when backward
682 * read-ahead is implemented */
683 LASSERTF(page_idx > ria->ria_stoff, "Invalid page_idx %lu"
684 "rs %lu re %lu ro %lu rl %lu rp %lu\n", page_idx,
685 ria->ria_start, ria->ria_end, ria->ria_stoff,
686 ria->ria_length, ria->ria_pages);
687 offset = page_idx - ria->ria_stoff;
688 offset = offset % (ria->ria_length);
689 if (offset > ria->ria_pages) {
690 page_idx += ria->ria_length - offset;
691 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
692 ria->ria_length - offset);
701 int ll_readahead(const struct lu_env *env, struct cl_io *io,
702 struct ll_readahead_state *ras, struct address_space *mapping,
703 struct cl_page_list *queue, int flags)
705 struct vvp_io *vio = vvp_env_io(env);
706 struct vvp_thread_info *vti = vvp_env_info(env);
707 struct cl_attr *attr = ccc_env_thread_attr(env);
708 unsigned long start = 0, end = 0, reserved;
709 unsigned long ra_end, len;
711 struct ll_ra_read *bead;
712 struct ra_io_arg *ria = &vti->vti_ria;
713 struct ll_inode_info *lli;
714 struct cl_object *clob;
719 inode = mapping->host;
720 lli = ll_i2info(inode);
721 clob = lli->lli_clob;
723 memset(ria, 0, sizeof *ria);
725 cl_object_attr_lock(clob);
726 ret = cl_object_attr_get(env, clob, attr);
727 cl_object_attr_unlock(clob);
733 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
737 cfs_spin_lock(&ras->ras_lock);
738 if (vio->cui_ra_window_set)
739 bead = &vio->cui_bead;
743 /* Enlarge the RA window to encompass the full read */
744 if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
745 bead->lrr_start + bead->lrr_count) {
746 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
747 ras->ras_window_start;
749 /* Reserve a part of the read-ahead window that we'll be issuing */
750 if (ras->ras_window_len) {
751 start = ras->ras_next_readahead;
752 end = ras->ras_window_start + ras->ras_window_len - 1;
755 unsigned long tmp_end;
757 * Align RA window to an optimal boundary.
759 * XXX This would be better to align to cl_max_pages_per_rpc
760 * instead of PTLRPC_MAX_BRW_PAGES, because the RPC size may
761 * be aligned to the RAID stripe size in the future and that
762 * is more important than the RPC size.
764 tmp_end = ((end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1))) - 1;
768 /* Truncate RA window to end of file */
769 end = min(end, (unsigned long)((kms - 1) >> CFS_PAGE_SHIFT));
771 ras->ras_next_readahead = max(end, end + 1);
774 ria->ria_start = start;
776 /* If stride I/O mode is detected, get stride window*/
777 if (stride_io_mode(ras)) {
778 ria->ria_stoff = ras->ras_stride_offset;
779 ria->ria_length = ras->ras_stride_length;
780 ria->ria_pages = ras->ras_stride_pages;
782 cfs_spin_unlock(&ras->ras_lock);
785 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
788 len = ria_page_count(ria);
792 reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len);
794 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
796 CDEBUG(D_READA, "reserved page %lu \n", reserved);
798 ret = ll_read_ahead_pages(env, io, queue,
799 ria, &reserved, mapping, &ra_end);
801 LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
803 ll_ra_count_put(ll_i2sbi(inode), reserved);
805 if (ra_end == end + 1 && ra_end == (kms >> CFS_PAGE_SHIFT))
806 ll_ra_stats_inc(mapping, RA_STAT_EOF);
808 /* if we didn't get to the end of the region we reserved from
809 * the ras we need to go back and update the ras so that the
810 * next read-ahead tries from where we left off. we only do so
811 * if the region we failed to issue read-ahead on is still ahead
812 * of the app and behind the next index to start read-ahead from */
813 CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
814 ra_end, end, ria->ria_end);
816 if (ra_end != end + 1) {
817 cfs_spin_lock(&ras->ras_lock);
818 if (ra_end < ras->ras_next_readahead &&
819 index_in_window(ra_end, ras->ras_window_start, 0,
820 ras->ras_window_len)) {
821 ras->ras_next_readahead = ra_end;
824 cfs_spin_unlock(&ras->ras_lock);
830 static void ras_set_start(struct ll_readahead_state *ras, unsigned long index)
832 ras->ras_window_start = index & (~(RAS_INCREASE_STEP - 1));
835 /* called with the ras_lock held or from places where it doesn't matter */
836 static void ras_reset(struct ll_readahead_state *ras, unsigned long index)
838 ras->ras_last_readpage = index;
839 ras->ras_consecutive_requests = 0;
840 ras->ras_consecutive_pages = 0;
841 ras->ras_window_len = 0;
842 ras_set_start(ras, index);
843 ras->ras_next_readahead = max(ras->ras_window_start, index);
848 /* called with the ras_lock held or from places where it doesn't matter */
849 static void ras_stride_reset(struct ll_readahead_state *ras)
851 ras->ras_consecutive_stride_requests = 0;
852 ras->ras_stride_length = 0;
853 ras->ras_stride_pages = 0;
857 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
859 cfs_spin_lock_init(&ras->ras_lock);
861 ras->ras_requests = 0;
862 CFS_INIT_LIST_HEAD(&ras->ras_read_beads);
866 * Check whether the read request is in the stride window.
867 * If it is in the stride window, return 1, otherwise return 0.
869 static int index_in_stride_window(unsigned long index,
870 struct ll_readahead_state *ras,
873 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
875 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
876 ras->ras_stride_pages == ras->ras_stride_length)
879 /* If it is contiguous read */
881 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
883 /*Otherwise check the stride by itself */
884 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
885 ras->ras_consecutive_pages == ras->ras_stride_pages;
888 static void ras_update_stride_detector(struct ll_readahead_state *ras,
891 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
893 if (!stride_io_mode(ras) && (stride_gap != 0 ||
894 ras->ras_consecutive_stride_requests == 0)) {
895 ras->ras_stride_pages = ras->ras_consecutive_pages;
896 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
898 LASSERT(ras->ras_request_index == 0);
899 LASSERT(ras->ras_consecutive_stride_requests == 0);
901 if (index <= ras->ras_last_readpage) {
902 /*Reset stride window for forward read*/
903 ras_stride_reset(ras);
907 ras->ras_stride_pages = ras->ras_consecutive_pages;
908 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
915 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
917 return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
918 ras->ras_stride_pages, ras->ras_stride_offset,
922 /* Stride Read-ahead window will be increased inc_len according to
923 * stride I/O pattern */
924 static void ras_stride_increase_window(struct ll_readahead_state *ras,
925 struct ll_ra_info *ra,
926 unsigned long inc_len)
928 unsigned long left, step, window_len;
929 unsigned long stride_len;
931 LASSERT(ras->ras_stride_length > 0);
932 LASSERTF(ras->ras_window_start + ras->ras_window_len
933 >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
934 " stride_offset %lu\n", ras->ras_window_start,
935 ras->ras_window_len, ras->ras_stride_offset);
937 stride_len = ras->ras_window_start + ras->ras_window_len -
938 ras->ras_stride_offset;
940 left = stride_len % ras->ras_stride_length;
941 window_len = ras->ras_window_len - left;
943 if (left < ras->ras_stride_pages)
946 left = ras->ras_stride_pages + inc_len;
948 LASSERT(ras->ras_stride_pages != 0);
950 step = left / ras->ras_stride_pages;
951 left %= ras->ras_stride_pages;
953 window_len += step * ras->ras_stride_length + left;
955 if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
956 ras->ras_window_len = window_len;
961 static void ras_increase_window(struct ll_readahead_state *ras,
962 struct ll_ra_info *ra, struct inode *inode)
964 /* The stretch of ra-window should be aligned with max rpc_size
965 * but current clio architecture does not support retrieve such
966 * information from lower layer. FIXME later
968 if (stride_io_mode(ras))
969 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP);
971 ras->ras_window_len = min(ras->ras_window_len +
973 ra->ra_max_pages_per_file);
976 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
977 struct ll_readahead_state *ras, unsigned long index,
980 struct ll_ra_info *ra = &sbi->ll_ra_info;
981 int zero = 0, stride_detect = 0, ra_miss = 0;
984 cfs_spin_lock(&ras->ras_lock);
986 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
988 /* reset the read-ahead window in two cases. First when the app seeks
989 * or reads to some other part of the file. Secondly if we get a
990 * read-ahead miss that we think we've previously issued. This can
991 * be a symptom of there being so many read-ahead pages that the VM is
992 * reclaiming it before we get to it. */
993 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
995 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
996 } else if (!hit && ras->ras_window_len &&
997 index < ras->ras_next_readahead &&
998 index_in_window(index, ras->ras_window_start, 0,
999 ras->ras_window_len)) {
1001 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
1004 /* On the second access to a file smaller than the tunable
1005 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
1006 * file up to ra_max_pages_per_file. This is simply a best effort
1007 * and only occurs once per open file. Normal RA behavior is reverted
1008 * to for subsequent IO. The mmap case does not increment
1009 * ras_requests and thus can never trigger this behavior. */
1010 if (ras->ras_requests == 2 && !ras->ras_request_index) {
1013 kms_pages = (i_size_read(inode) + CFS_PAGE_SIZE - 1) >>
1016 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
1017 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
1020 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
1021 ras->ras_window_start = 0;
1022 ras->ras_last_readpage = 0;
1023 ras->ras_next_readahead = 0;
1024 ras->ras_window_len = min(ra->ra_max_pages_per_file,
1025 ra->ra_max_read_ahead_whole_pages);
1026 GOTO(out_unlock, 0);
1030 /* check whether it is in stride I/O mode*/
1031 if (!index_in_stride_window(index, ras, inode)) {
1032 if (ras->ras_consecutive_stride_requests == 0 &&
1033 ras->ras_request_index == 0) {
1034 ras_update_stride_detector(ras, index);
1035 ras->ras_consecutive_stride_requests ++;
1037 ras_stride_reset(ras);
1039 ras_reset(ras, index);
1040 ras->ras_consecutive_pages++;
1041 GOTO(out_unlock, 0);
1043 ras->ras_consecutive_pages = 0;
1044 ras->ras_consecutive_requests = 0;
1045 if (++ras->ras_consecutive_stride_requests > 1)
1051 if (index_in_stride_window(index, ras, inode) &&
1052 stride_io_mode(ras)) {
1053 /*If stride-RA hit cache miss, the stride dector
1054 *will not be reset to avoid the overhead of
1055 *redetecting read-ahead mode */
1056 if (index != ras->ras_last_readpage + 1)
1057 ras->ras_consecutive_pages = 0;
1058 ras_reset(ras, index);
1061 /* Reset both stride window and normal RA
1063 ras_reset(ras, index);
1064 ras->ras_consecutive_pages++;
1065 ras_stride_reset(ras);
1066 GOTO(out_unlock, 0);
1068 } else if (stride_io_mode(ras)) {
1069 /* If this is contiguous read but in stride I/O mode
1070 * currently, check whether stride step still is valid,
1071 * if invalid, it will reset the stride ra window*/
1072 if (!index_in_stride_window(index, ras, inode)) {
1073 /* Shrink stride read-ahead window to be zero */
1074 ras_stride_reset(ras);
1075 ras->ras_window_len = 0;
1076 ras->ras_next_readahead = index;
1080 ras->ras_consecutive_pages++;
1081 ras->ras_last_readpage = index;
1082 ras_set_start(ras, index);
1084 if (stride_io_mode(ras))
1085 /* Since stride readahead is sentivite to the offset
1086 * of read-ahead, so we use original offset here,
1087 * instead of ras_window_start, which is 1M aligned*/
1088 ras->ras_next_readahead = max(index,
1089 ras->ras_next_readahead);
1091 ras->ras_next_readahead = max(ras->ras_window_start,
1092 ras->ras_next_readahead);
1095 /* Trigger RA in the mmap case where ras_consecutive_requests
1096 * is not incremented and thus can't be used to trigger RA */
1097 if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
1098 ras->ras_window_len = RAS_INCREASE_STEP;
1099 GOTO(out_unlock, 0);
1102 /* Initially reset the stride window offset to next_readahead*/
1103 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
1105 * Once stride IO mode is detected, next_readahead should be
1106 * reset to make sure next_readahead > stride offset
1108 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
1109 ras->ras_stride_offset = index;
1110 ras->ras_window_len = RAS_INCREASE_STEP;
1113 /* The initial ras_window_len is set to the request size. To avoid
1114 * uselessly reading and discarding pages for random IO the window is
1115 * only increased once per consecutive request received. */
1116 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
1117 !ras->ras_request_index)
1118 ras_increase_window(ras, ra, inode);
1122 ras->ras_request_index++;
1123 cfs_spin_unlock(&ras->ras_lock);
1127 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1129 struct inode *inode = vmpage->mapping->host;
1132 struct cl_page *page;
1133 struct cl_object *clob;
1134 struct cl_2queue *queue;
1135 struct cl_env_nest nest;
1139 LASSERT(PageLocked(vmpage));
1140 LASSERT(!PageWriteback(vmpage));
1142 if (ll_i2dtexp(inode) == NULL)
1145 env = cl_env_nested_get(&nest);
1147 RETURN(PTR_ERR(env));
1149 queue = &vvp_env_info(env)->vti_queue;
1150 clob = ll_i2info(inode)->lli_clob;
1151 LASSERT(clob != NULL);
1153 io = ccc_env_thread_io(env);
1155 result = cl_io_init(env, io, CIT_MISC, clob);
1157 page = cl_page_find(env, clob, vmpage->index,
1158 vmpage, CPT_CACHEABLE);
1159 if (!IS_ERR(page)) {
1160 lu_ref_add(&page->cp_reference, "writepage",
1162 cl_page_assume(env, io, page);
1164 * Mark page dirty, because this is what
1165 * ->vio_submit()->cpo_prep_write() assumes.
1167 * XXX better solution is to detect this from within
1168 * cl_io_submit_rw() somehow.
1170 set_page_dirty(vmpage);
1171 cl_2queue_init_page(queue, page);
1172 result = cl_io_submit_rw(env, io, CRT_WRITE,
1176 * Re-dirty page on error so it retries write,
1177 * but not in case when IO has actually
1178 * occurred and completed with an error.
1180 if (!PageError(vmpage)) {
1181 redirty_page_for_writepage(wbc, vmpage);
1185 cl_page_list_disown(env, io, &queue->c2_qin);
1186 LASSERT(!cl_page_is_owned(page, io));
1187 lu_ref_del(&page->cp_reference,
1188 "writepage", cfs_current());
1189 cl_page_put(env, page);
1190 cl_2queue_fini(env, queue);
1193 cl_io_fini(env, io);
1194 cl_env_nested_put(&nest, env);
1198 int ll_readpage(struct file *file, struct page *vmpage)
1200 struct ll_cl_context *lcc;
1204 lcc = ll_cl_init(file, vmpage, 0);
1206 struct lu_env *env = lcc->lcc_env;
1207 struct cl_io *io = lcc->lcc_io;
1208 struct cl_page *page = lcc->lcc_page;
1210 LASSERT(page->cp_type == CPT_CACHEABLE);
1211 if (likely(!PageUptodate(vmpage))) {
1212 cl_page_assume(env, io, page);
1213 result = cl_io_read_page(env, io, page);
1215 /* Page from a non-object file. */
1216 LASSERT(!ll_i2info(vmpage->mapping->host)->lli_smd);
1217 unlock_page(vmpage);
1222 unlock_page(vmpage);
1223 result = PTR_ERR(lcc);