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.
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/autoconf.h>
42 #include <linux/kernel.h>
44 #include <linux/string.h>
45 #include <linux/stat.h>
46 #include <linux/errno.h>
47 #include <linux/smp_lock.h>
48 #include <linux/unistd.h>
49 #include <linux/version.h>
50 #include <asm/system.h>
51 #include <asm/uaccess.h>
54 #include <linux/stat.h>
55 #include <asm/uaccess.h>
57 #include <linux/pagemap.h>
58 #include <linux/smp_lock.h>
59 /* current_is_kswapd() */
60 #include <linux/swap.h>
62 #define DEBUG_SUBSYSTEM S_LLITE
64 //#include <lustre_mdc.h>
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)
78 struct ll_inode_info *lli = ll_i2info(inode);
81 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) to %Lu\n",inode->i_ino,
82 inode->i_generation, inode, i_size_read(inode));
84 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_TRUNC, 1);
85 if (lli->lli_size_sem_owner == cfs_current()) {
86 LASSERT_SEM_LOCKED(&lli->lli_size_sem);
87 ll_inode_size_unlock(inode, 0);
95 * Finalizes cl-data before exiting typical address_space operation. Dual to
98 static void ll_cl_fini(struct ll_cl_context *lcc)
100 struct lu_env *env = lcc->lcc_env;
101 struct cl_io *io = lcc->lcc_io;
102 struct cl_page *page = lcc->lcc_page;
104 LASSERT(lcc->lcc_cookie == current);
105 LASSERT(env != NULL);
108 lu_ref_del(&page->cp_reference, "cl_io", io);
109 cl_page_put(env, page);
112 if (io && lcc->lcc_created) {
114 cl_io_unlock(env, io);
115 cl_io_iter_fini(env, io);
118 cl_env_put(env, &lcc->lcc_refcheck);
122 * Initializes common cl-data at the typical address_space operation entry
125 static struct ll_cl_context *ll_cl_init(struct file *file,
126 struct page *vmpage, int create)
128 struct ll_cl_context *lcc;
131 struct cl_object *clob;
137 clob = ll_i2info(vmpage->mapping->host)->lli_clob;
138 LASSERT(clob != NULL);
140 env = cl_env_get(&refcheck);
142 return ERR_PTR(PTR_ERR(env));
144 lcc = &vvp_env_info(env)->vti_io_ctx;
145 memset(lcc, 0, sizeof(*lcc));
147 lcc->lcc_refcheck = refcheck;
148 lcc->lcc_cookie = current;
150 cio = ccc_env_io(env);
151 io = cio->cui_cl.cis_io;
152 if (io == NULL && create) {
157 * Loop-back driver calls ->prepare_write() and ->sendfile()
158 * methods directly, bypassing file system ->write() operation,
159 * so cl_io has to be created here.
162 io = ccc_env_thread_io(env);
163 vio = vvp_env_io(env);
164 ll_io_init(io, file, 1);
166 /* No lock at all for this kind of IO - we can't do it because
167 * we have held page lock, it would cause deadlock.
168 * XXX: This causes poor performance to loop device - One page
170 * In order to get better performance, users should use
171 * lloop driver instead.
173 io->ci_lockreq = CILR_NEVER;
175 pos = (vmpage->index << CFS_PAGE_SHIFT);
177 /* Create a temp IO to serve write. */
178 result = cl_io_rw_init(env, io, CIT_WRITE, pos, CFS_PAGE_SIZE);
180 cio->cui_fd = LUSTRE_FPRIVATE(file);
183 result = cl_io_iter_init(env, io);
185 result = cl_io_lock(env, io);
187 result = cl_io_start(env, io);
190 result = io->ci_result;
191 lcc->lcc_created = 1;
198 struct cl_page *page;
201 LASSERT(io->ci_state == CIS_IO_GOING);
202 LASSERT(cio->cui_fd == LUSTRE_FPRIVATE(file));
203 page = cl_page_find(env, clob, vmpage->index, vmpage,
206 lcc->lcc_page = page;
207 lu_ref_add(&page->cp_reference, "cl_io", io);
210 result = PTR_ERR(page);
214 lcc = ERR_PTR(result);
217 CDEBUG(D_VFSTRACE, "%lu@"DFID" -> %d %p %p\n",
218 vmpage->index, PFID(lu_object_fid(&clob->co_lu)), result,
223 static struct ll_cl_context *ll_cl_get(void)
225 struct ll_cl_context *lcc;
229 env = cl_env_get(&refcheck);
230 LASSERT(!IS_ERR(env));
231 lcc = &vvp_env_info(env)->vti_io_ctx;
232 LASSERT(env == lcc->lcc_env);
233 LASSERT(current == lcc->lcc_cookie);
234 cl_env_put(env, &refcheck);
236 /* env has got in ll_cl_init, so it is still usable. */
241 * ->prepare_write() address space operation called by generic_file_write()
242 * for every page during write.
244 int ll_prepare_write(struct file *file, struct page *vmpage, unsigned from,
247 struct ll_cl_context *lcc;
251 lcc = ll_cl_init(file, vmpage, 1);
253 struct lu_env *env = lcc->lcc_env;
254 struct cl_io *io = lcc->lcc_io;
255 struct cl_page *page = lcc->lcc_page;
257 cl_page_assume(env, io, page);
258 if (cl_io_is_append(io)) {
259 struct cl_object *obj = io->ci_obj;
260 struct inode *inode = ccc_object_inode(obj);
262 * In VFS file->page write loop, for appending, the
263 * write offset might be reset according to the new
264 * file size before holding i_mutex. So crw_pos should
265 * be reset here. BUG:17711.
267 io->u.ci_wr.wr.crw_pos = i_size_read(inode);
269 result = cl_io_prepare_write(env, io, page, from, to);
272 * Add a reference, so that page is not evicted from
273 * the cache until ->commit_write() is called.
276 lu_ref_add(&page->cp_reference, "prepare_write",
279 cl_page_unassume(env, io, page);
282 /* returning 0 in prepare assumes commit must be called
285 result = PTR_ERR(lcc);
290 int ll_commit_write(struct file *file, struct page *vmpage, unsigned from,
293 struct ll_cl_context *lcc;
296 struct cl_page *page;
302 page = lcc->lcc_page;
305 LASSERT(cl_page_is_owned(page, io));
307 if (from != to) /* handle short write case. */
308 result = cl_io_commit_write(env, io, page, from, to);
309 if (cl_page_is_owned(page, io))
310 cl_page_unassume(env, io, page);
313 * Release reference acquired by ll_prepare_write().
315 lu_ref_del(&page->cp_reference, "prepare_write", cfs_current());
316 cl_page_put(env, page);
321 struct obd_capa *cl_capa_lookup(struct inode *inode, enum cl_req_type crt)
325 opc = crt == CRT_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
326 return ll_osscapa_get(inode, opc);
329 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
331 /* WARNING: This algorithm is used to reduce the contention on
332 * sbi->ll_lock. It should work well if the ra_max_pages is much
333 * greater than the single file's read-ahead window.
335 * TODO: There may exist a `global sync problem' in this implementation.
336 * Considering the global ra window is 100M, and each file's ra window is 10M,
337 * there are over 10 files trying to get its ra budget and reach
338 * ll_ra_count_get at the exactly same time. All of them will get a zero ra
339 * window, although the global window is 100M. -jay
341 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi, unsigned long len)
343 struct ll_ra_info *ra = &sbi->ll_ra_info;
348 * If read-ahead pages left are less than 1M, do not do read-ahead,
349 * otherwise it will form small read RPC(< 1M), which hurt server
352 ret = min(ra->ra_max_pages - cfs_atomic_read(&ra->ra_cur_pages), len);
353 if ((int)ret < 0 || ret < min((unsigned long)PTLRPC_MAX_BRW_PAGES, len))
356 if (cfs_atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
357 cfs_atomic_sub(ret, &ra->ra_cur_pages);
364 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
366 struct ll_ra_info *ra = &sbi->ll_ra_info;
367 cfs_atomic_sub(len, &ra->ra_cur_pages);
370 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
372 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
373 lprocfs_counter_incr(sbi->ll_ra_stats, which);
376 void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
378 struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
379 ll_ra_stats_inc_sbi(sbi, which);
382 #define RAS_CDEBUG(ras) \
384 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu" \
385 "csr %lu sf %lu sp %lu sl %lu \n", \
386 ras->ras_last_readpage, ras->ras_consecutive_requests, \
387 ras->ras_consecutive_pages, ras->ras_window_start, \
388 ras->ras_window_len, ras->ras_next_readahead, \
389 ras->ras_requests, ras->ras_request_index, \
390 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
391 ras->ras_stride_pages, ras->ras_stride_length)
393 static int index_in_window(unsigned long index, unsigned long point,
394 unsigned long before, unsigned long after)
396 unsigned long start = point - before, end = point + after;
403 return start <= index && index <= end;
406 static struct ll_readahead_state *ll_ras_get(struct file *f)
408 struct ll_file_data *fd;
410 fd = LUSTRE_FPRIVATE(f);
414 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar)
416 struct ll_readahead_state *ras;
420 cfs_spin_lock(&ras->ras_lock);
422 ras->ras_request_index = 0;
423 ras->ras_consecutive_requests++;
424 rar->lrr_reader = current;
426 cfs_list_add(&rar->lrr_linkage, &ras->ras_read_beads);
427 cfs_spin_unlock(&ras->ras_lock);
430 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
432 struct ll_readahead_state *ras;
436 cfs_spin_lock(&ras->ras_lock);
437 cfs_list_del_init(&rar->lrr_linkage);
438 cfs_spin_unlock(&ras->ras_lock);
441 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
443 struct ll_ra_read *scan;
445 cfs_list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
446 if (scan->lrr_reader == current)
452 struct ll_ra_read *ll_ra_read_get(struct file *f)
454 struct ll_readahead_state *ras;
455 struct ll_ra_read *bead;
459 cfs_spin_lock(&ras->ras_lock);
460 bead = ll_ra_read_get_locked(ras);
461 cfs_spin_unlock(&ras->ras_lock);
465 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
466 struct cl_page_list *queue, struct cl_page *page,
475 cl_page_assume(env, io, page);
476 lu_ref_add(&page->cp_reference, "ra", cfs_current());
477 cp = cl2ccc_page(cl_page_at(page, &vvp_device_type));
478 if (!cp->cpg_defer_uptodate && !Page_Uptodate(vmpage)) {
479 rc = cl_page_is_under_lock(env, io, page);
481 cp->cpg_defer_uptodate = 1;
483 cl_page_list_add(queue, page);
486 cl_page_delete(env, page);
490 /* skip completed pages */
491 cl_page_unassume(env, io, page);
492 lu_ref_del(&page->cp_reference, "ra", cfs_current());
493 cl_page_put(env, page);
498 * Initiates read-ahead of a page with given index.
500 * \retval +ve: page was added to \a queue.
502 * \retval -ENOLCK: there is no extent lock for this part of a file, stop
505 * \retval -ve, 0: page wasn't added to \a queue for other reason.
507 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
508 struct cl_page_list *queue,
509 pgoff_t index, struct address_space *mapping)
512 struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
513 struct cl_page *page;
514 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
515 unsigned int gfp_mask;
517 const char *msg = NULL;
521 gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
523 gfp_mask |= __GFP_NOWARN;
525 vmpage = grab_cache_page_nowait_gfp(mapping, index, gfp_mask);
526 if (vmpage != NULL) {
527 /* Check if vmpage was truncated or reclaimed */
528 if (vmpage->mapping == mapping) {
529 page = cl_page_find(env, clob, vmpage->index,
530 vmpage, CPT_CACHEABLE);
532 rc = cl_read_ahead_page(env, io, queue,
535 which = RA_STAT_FAILED_MATCH;
536 msg = "lock match failed";
539 which = RA_STAT_FAILED_GRAB_PAGE;
540 msg = "cl_page_find failed";
543 which = RA_STAT_WRONG_GRAB_PAGE;
544 msg = "g_c_p_n returned invalid page";
548 page_cache_release(vmpage);
550 which = RA_STAT_FAILED_GRAB_PAGE;
551 msg = "g_c_p_n failed";
554 ll_ra_stats_inc(mapping, which);
555 CDEBUG(D_READA, "%s\n", msg);
560 #define RIA_DEBUG(ria) \
561 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
562 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
565 #define RAS_INCREASE_STEP PTLRPC_MAX_BRW_PAGES
567 static inline int stride_io_mode(struct ll_readahead_state *ras)
569 return ras->ras_consecutive_stride_requests > 1;
571 /* The function calculates how much pages will be read in
572 * [off, off + length], in such stride IO area,
573 * stride_offset = st_off, stride_lengh = st_len,
574 * stride_pages = st_pgs
576 * |------------------|*****|------------------|*****|------------|*****|....
579 * |----- st_len -----|
581 * How many pages it should read in such pattern
582 * |-------------------------------------------------------------|
584 * |<------ length ------->|
586 * = |<----->| + |-------------------------------------| + |---|
587 * start_left st_pgs * i end_left
590 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
591 unsigned long off, unsigned long length)
593 __u64 start = off > st_off ? off - st_off : 0;
594 __u64 end = off + length > st_off ? off + length - st_off : 0;
595 unsigned long start_left = 0;
596 unsigned long end_left = 0;
597 unsigned long pg_count;
599 if (st_len == 0 || length == 0 || end == 0)
602 start_left = do_div(start, st_len);
603 if (start_left < st_pgs)
604 start_left = st_pgs - start_left;
608 end_left = do_div(end, st_len);
609 if (end_left > st_pgs)
612 CDEBUG(D_READA, "start "LPU64", end "LPU64" start_left %lu end_left %lu \n",
613 start, end, start_left, end_left);
616 pg_count = end_left - (st_pgs - start_left);
618 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
620 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
621 "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
626 static int ria_page_count(struct ra_io_arg *ria)
628 __u64 length = ria->ria_end >= ria->ria_start ?
629 ria->ria_end - ria->ria_start + 1 : 0;
631 return stride_pg_count(ria->ria_stoff, ria->ria_length,
632 ria->ria_pages, ria->ria_start,
636 /*Check whether the index is in the defined ra-window */
637 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
639 /* If ria_length == ria_pages, it means non-stride I/O mode,
640 * idx should always inside read-ahead window in this case
641 * For stride I/O mode, just check whether the idx is inside
643 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
644 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
645 ria->ria_length < ria->ria_pages);
648 static int ll_read_ahead_pages(const struct lu_env *env,
649 struct cl_io *io, struct cl_page_list *queue,
650 struct ra_io_arg *ria,
651 unsigned long *reserved_pages,
652 struct address_space *mapping,
653 unsigned long *ra_end)
655 int rc, count = 0, stride_ria;
656 unsigned long page_idx;
658 LASSERT(ria != NULL);
661 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
662 for (page_idx = ria->ria_start; page_idx <= ria->ria_end &&
663 *reserved_pages > 0; page_idx++) {
664 if (ras_inside_ra_window(page_idx, ria)) {
665 /* If the page is inside the read-ahead window*/
666 rc = ll_read_ahead_page(env, io, queue,
671 } else if (rc == -ENOLCK)
673 } else if (stride_ria) {
674 /* If it is not in the read-ahead window, and it is
675 * read-ahead mode, then check whether it should skip
678 /* FIXME: This assertion only is valid when it is for
679 * forward read-ahead, it will be fixed when backward
680 * read-ahead is implemented */
681 LASSERTF(page_idx > ria->ria_stoff, "Invalid page_idx %lu"
682 "rs %lu re %lu ro %lu rl %lu rp %lu\n", page_idx,
683 ria->ria_start, ria->ria_end, ria->ria_stoff,
684 ria->ria_length, ria->ria_pages);
685 offset = page_idx - ria->ria_stoff;
686 offset = offset % (ria->ria_length);
687 if (offset > ria->ria_pages) {
688 page_idx += ria->ria_length - offset;
689 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
690 ria->ria_length - offset);
699 int ll_readahead(const struct lu_env *env, struct cl_io *io,
700 struct ll_readahead_state *ras, struct address_space *mapping,
701 struct cl_page_list *queue, int flags)
703 struct vvp_io *vio = vvp_env_io(env);
704 struct vvp_thread_info *vti = vvp_env_info(env);
705 struct cl_attr *attr = ccc_env_thread_attr(env);
706 unsigned long start = 0, end = 0, reserved;
707 unsigned long ra_end, len;
709 struct ll_ra_read *bead;
710 struct ra_io_arg *ria = &vti->vti_ria;
711 struct ll_inode_info *lli;
712 struct cl_object *clob;
717 inode = mapping->host;
718 lli = ll_i2info(inode);
719 clob = lli->lli_clob;
721 memset(ria, 0, sizeof *ria);
723 cl_object_attr_lock(clob);
724 ret = cl_object_attr_get(env, clob, attr);
725 cl_object_attr_unlock(clob);
731 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
735 cfs_spin_lock(&ras->ras_lock);
736 if (vio->cui_ra_window_set)
737 bead = &vio->cui_bead;
741 /* Enlarge the RA window to encompass the full read */
742 if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
743 bead->lrr_start + bead->lrr_count) {
744 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
745 ras->ras_window_start;
747 /* Reserve a part of the read-ahead window that we'll be issuing */
748 if (ras->ras_window_len) {
749 start = ras->ras_next_readahead;
750 end = ras->ras_window_start + ras->ras_window_len - 1;
753 unsigned long tmp_end;
755 * Align RA window to an optimal boundary.
757 * XXX This would be better to align to cl_max_pages_per_rpc
758 * instead of PTLRPC_MAX_BRW_PAGES, because the RPC size may
759 * be aligned to the RAID stripe size in the future and that
760 * is more important than the RPC size.
762 tmp_end = ((end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1))) - 1;
766 /* Truncate RA window to end of file */
767 end = min(end, (unsigned long)((kms - 1) >> CFS_PAGE_SHIFT));
769 ras->ras_next_readahead = max(end, end + 1);
772 ria->ria_start = start;
774 /* If stride I/O mode is detected, get stride window*/
775 if (stride_io_mode(ras)) {
776 ria->ria_stoff = ras->ras_stride_offset;
777 ria->ria_length = ras->ras_stride_length;
778 ria->ria_pages = ras->ras_stride_pages;
780 cfs_spin_unlock(&ras->ras_lock);
783 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
786 len = ria_page_count(ria);
790 reserved = ll_ra_count_get(ll_i2sbi(inode), len);
793 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
795 CDEBUG(D_READA, "reserved page %lu \n", reserved);
797 ret = ll_read_ahead_pages(env, io, queue,
798 ria, &reserved, mapping, &ra_end);
800 LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
802 ll_ra_count_put(ll_i2sbi(inode), reserved);
804 if (ra_end == end + 1 && ra_end == (kms >> CFS_PAGE_SHIFT))
805 ll_ra_stats_inc(mapping, RA_STAT_EOF);
807 /* if we didn't get to the end of the region we reserved from
808 * the ras we need to go back and update the ras so that the
809 * next read-ahead tries from where we left off. we only do so
810 * if the region we failed to issue read-ahead on is still ahead
811 * of the app and behind the next index to start read-ahead from */
812 CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
813 ra_end, end, ria->ria_end);
815 if (ra_end != end + 1) {
816 cfs_spin_lock(&ras->ras_lock);
817 if (ra_end < ras->ras_next_readahead &&
818 index_in_window(ra_end, ras->ras_window_start, 0,
819 ras->ras_window_len)) {
820 ras->ras_next_readahead = ra_end;
823 cfs_spin_unlock(&ras->ras_lock);
829 static void ras_set_start(struct ll_readahead_state *ras, unsigned long index)
831 ras->ras_window_start = index & (~(RAS_INCREASE_STEP - 1));
834 /* called with the ras_lock held or from places where it doesn't matter */
835 static void ras_reset(struct ll_readahead_state *ras, unsigned long index)
837 ras->ras_last_readpage = index;
838 ras->ras_consecutive_requests = 0;
839 ras->ras_consecutive_pages = 0;
840 ras->ras_window_len = 0;
841 ras_set_start(ras, index);
842 ras->ras_next_readahead = max(ras->ras_window_start, index);
847 /* called with the ras_lock held or from places where it doesn't matter */
848 static void ras_stride_reset(struct ll_readahead_state *ras)
850 ras->ras_consecutive_stride_requests = 0;
851 ras->ras_stride_length = 0;
852 ras->ras_stride_pages = 0;
856 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
858 cfs_spin_lock_init(&ras->ras_lock);
860 ras->ras_requests = 0;
861 CFS_INIT_LIST_HEAD(&ras->ras_read_beads);
865 * Check whether the read request is in the stride window.
866 * If it is in the stride window, return 1, otherwise return 0.
868 static int index_in_stride_window(unsigned long index,
869 struct ll_readahead_state *ras,
872 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
874 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
875 ras->ras_stride_pages == ras->ras_stride_length)
878 /* If it is contiguous read */
880 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
882 /*Otherwise check the stride by itself */
883 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
884 ras->ras_consecutive_pages == ras->ras_stride_pages;
887 static void ras_update_stride_detector(struct ll_readahead_state *ras,
890 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
892 if (!stride_io_mode(ras) && (stride_gap != 0 ||
893 ras->ras_consecutive_stride_requests == 0)) {
894 ras->ras_stride_pages = ras->ras_consecutive_pages;
895 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
897 LASSERT(ras->ras_request_index == 0);
898 LASSERT(ras->ras_consecutive_stride_requests == 0);
900 if (index <= ras->ras_last_readpage) {
901 /*Reset stride window for forward read*/
902 ras_stride_reset(ras);
906 ras->ras_stride_pages = ras->ras_consecutive_pages;
907 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
914 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
916 return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
917 ras->ras_stride_pages, ras->ras_stride_offset,
921 /* Stride Read-ahead window will be increased inc_len according to
922 * stride I/O pattern */
923 static void ras_stride_increase_window(struct ll_readahead_state *ras,
924 struct ll_ra_info *ra,
925 unsigned long inc_len)
927 unsigned long left, step, window_len;
928 unsigned long stride_len;
930 LASSERT(ras->ras_stride_length > 0);
931 LASSERTF(ras->ras_window_start + ras->ras_window_len
932 >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
933 " stride_offset %lu\n", ras->ras_window_start,
934 ras->ras_window_len, ras->ras_stride_offset);
936 stride_len = ras->ras_window_start + ras->ras_window_len -
937 ras->ras_stride_offset;
939 left = stride_len % ras->ras_stride_length;
940 window_len = ras->ras_window_len - left;
942 if (left < ras->ras_stride_pages)
945 left = ras->ras_stride_pages + inc_len;
947 LASSERT(ras->ras_stride_pages != 0);
949 step = left / ras->ras_stride_pages;
950 left %= ras->ras_stride_pages;
952 window_len += step * ras->ras_stride_length + left;
954 if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
955 ras->ras_window_len = window_len;
960 static void ras_increase_window(struct ll_readahead_state *ras,
961 struct ll_ra_info *ra, struct inode *inode)
963 /* The stretch of ra-window should be aligned with max rpc_size
964 * but current clio architecture does not support retrieve such
965 * information from lower layer. FIXME later
967 if (stride_io_mode(ras))
968 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP);
970 ras->ras_window_len = min(ras->ras_window_len +
972 ra->ra_max_pages_per_file);
975 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
976 struct ll_readahead_state *ras, unsigned long index,
979 struct ll_ra_info *ra = &sbi->ll_ra_info;
980 int zero = 0, stride_detect = 0, ra_miss = 0;
983 cfs_spin_lock(&ras->ras_lock);
985 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
987 /* reset the read-ahead window in two cases. First when the app seeks
988 * or reads to some other part of the file. Secondly if we get a
989 * read-ahead miss that we think we've previously issued. This can
990 * be a symptom of there being so many read-ahead pages that the VM is
991 * reclaiming it before we get to it. */
992 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
994 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
995 } else if (!hit && ras->ras_window_len &&
996 index < ras->ras_next_readahead &&
997 index_in_window(index, ras->ras_window_start, 0,
998 ras->ras_window_len)) {
1000 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
1003 /* On the second access to a file smaller than the tunable
1004 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
1005 * file up to ra_max_pages_per_file. This is simply a best effort
1006 * and only occurs once per open file. Normal RA behavior is reverted
1007 * to for subsequent IO. The mmap case does not increment
1008 * ras_requests and thus can never trigger this behavior. */
1009 if (ras->ras_requests == 2 && !ras->ras_request_index) {
1012 kms_pages = (i_size_read(inode) + CFS_PAGE_SIZE - 1) >>
1015 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
1016 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
1019 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
1020 ras->ras_window_start = 0;
1021 ras->ras_last_readpage = 0;
1022 ras->ras_next_readahead = 0;
1023 ras->ras_window_len = min(ra->ra_max_pages_per_file,
1024 ra->ra_max_read_ahead_whole_pages);
1025 GOTO(out_unlock, 0);
1029 /* check whether it is in stride I/O mode*/
1030 if (!index_in_stride_window(index, ras, inode)) {
1031 if (ras->ras_consecutive_stride_requests == 0 &&
1032 ras->ras_request_index == 0) {
1033 ras_update_stride_detector(ras, index);
1034 ras->ras_consecutive_stride_requests ++;
1036 ras_stride_reset(ras);
1038 ras_reset(ras, index);
1039 ras->ras_consecutive_pages++;
1040 GOTO(out_unlock, 0);
1042 ras->ras_consecutive_pages = 0;
1043 ras->ras_consecutive_requests = 0;
1044 if (++ras->ras_consecutive_stride_requests > 1)
1050 if (index_in_stride_window(index, ras, inode) &&
1051 stride_io_mode(ras)) {
1052 /*If stride-RA hit cache miss, the stride dector
1053 *will not be reset to avoid the overhead of
1054 *redetecting read-ahead mode */
1055 if (index != ras->ras_last_readpage + 1)
1056 ras->ras_consecutive_pages = 0;
1057 ras_reset(ras, index);
1060 /* Reset both stride window and normal RA
1062 ras_reset(ras, index);
1063 ras->ras_consecutive_pages++;
1064 ras_stride_reset(ras);
1065 GOTO(out_unlock, 0);
1067 } else if (stride_io_mode(ras)) {
1068 /* If this is contiguous read but in stride I/O mode
1069 * currently, check whether stride step still is valid,
1070 * if invalid, it will reset the stride ra window*/
1071 if (!index_in_stride_window(index, ras, inode)) {
1072 /* Shrink stride read-ahead window to be zero */
1073 ras_stride_reset(ras);
1074 ras->ras_window_len = 0;
1075 ras->ras_next_readahead = index;
1079 ras->ras_consecutive_pages++;
1080 ras->ras_last_readpage = index;
1081 ras_set_start(ras, index);
1083 if (stride_io_mode(ras))
1084 /* Since stride readahead is sentivite to the offset
1085 * of read-ahead, so we use original offset here,
1086 * instead of ras_window_start, which is 1M aligned*/
1087 ras->ras_next_readahead = max(index,
1088 ras->ras_next_readahead);
1090 ras->ras_next_readahead = max(ras->ras_window_start,
1091 ras->ras_next_readahead);
1094 /* Trigger RA in the mmap case where ras_consecutive_requests
1095 * is not incremented and thus can't be used to trigger RA */
1096 if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
1097 ras->ras_window_len = RAS_INCREASE_STEP;
1098 GOTO(out_unlock, 0);
1101 /* Initially reset the stride window offset to next_readahead*/
1102 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
1104 * Once stride IO mode is detected, next_readahead should be
1105 * reset to make sure next_readahead > stride offset
1107 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
1108 ras->ras_stride_offset = index;
1109 ras->ras_window_len = RAS_INCREASE_STEP;
1112 /* The initial ras_window_len is set to the request size. To avoid
1113 * uselessly reading and discarding pages for random IO the window is
1114 * only increased once per consecutive request received. */
1115 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
1116 !ras->ras_request_index)
1117 ras_increase_window(ras, ra, inode);
1121 ras->ras_request_index++;
1122 cfs_spin_unlock(&ras->ras_lock);
1126 int ll_writepage(struct page *vmpage, struct writeback_control *unused)
1128 struct inode *inode = vmpage->mapping->host;
1131 struct cl_page *page;
1132 struct cl_object *clob;
1133 struct cl_2queue *queue;
1134 struct cl_env_nest nest;
1138 LASSERT(PageLocked(vmpage));
1139 LASSERT(!PageWriteback(vmpage));
1141 if (ll_i2dtexp(inode) == NULL)
1144 env = cl_env_nested_get(&nest);
1146 RETURN(PTR_ERR(env));
1148 queue = &vvp_env_info(env)->vti_queue;
1149 clob = ll_i2info(inode)->lli_clob;
1150 LASSERT(clob != NULL);
1152 io = ccc_env_thread_io(env);
1154 result = cl_io_init(env, io, CIT_MISC, clob);
1156 page = cl_page_find(env, clob, vmpage->index,
1157 vmpage, CPT_CACHEABLE);
1158 if (!IS_ERR(page)) {
1159 lu_ref_add(&page->cp_reference, "writepage",
1161 cl_page_assume(env, io, page);
1163 * Mark page dirty, because this is what
1164 * ->vio_submit()->cpo_prep_write() assumes.
1166 * XXX better solution is to detect this from within
1167 * cl_io_submit_rw() somehow.
1169 set_page_dirty(vmpage);
1170 cl_2queue_init_page(queue, page);
1171 result = cl_io_submit_rw(env, io, CRT_WRITE,
1173 cl_page_list_disown(env, io, &queue->c2_qin);
1176 * There is no need to clear PG_writeback, as
1177 * cl_io_submit_rw() calls completion callback
1181 * Re-dirty page on error so it retries write,
1182 * but not in case when IO has actually
1183 * occurred and completed with an error.
1185 if (!PageError(vmpage))
1186 set_page_dirty(vmpage);
1188 LASSERT(!cl_page_is_owned(page, io));
1189 lu_ref_del(&page->cp_reference,
1190 "writepage", cfs_current());
1191 cl_page_put(env, page);
1192 cl_2queue_fini(env, queue);
1195 cl_io_fini(env, io);
1196 cl_env_nested_put(&nest, env);
1200 int ll_readpage(struct file *file, struct page *vmpage)
1202 struct ll_cl_context *lcc;
1206 lcc = ll_cl_init(file, vmpage, 0);
1208 struct lu_env *env = lcc->lcc_env;
1209 struct cl_io *io = lcc->lcc_io;
1210 struct cl_page *page = lcc->lcc_page;
1212 LASSERT(page->cp_type == CPT_CACHEABLE);
1213 if (likely(!PageUptodate(vmpage))) {
1214 cl_page_assume(env, io, page);
1215 result = cl_io_read_page(env, io, page);
1217 /* Page from a non-object file. */
1218 LASSERT(!ll_i2info(vmpage->mapping->host)->lli_smd);
1219 unlock_page(vmpage);
1224 unlock_page(vmpage);
1225 result = PTR_ERR(lcc);