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 2008 Sun Microsystems, Inc. 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=%#Lx\n",inode->i_ino,
82 inode->i_generation, inode, i_size_read(inode),
85 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_TRUNC, 1);
86 if (lli->lli_size_sem_owner != cfs_current()) {
92 CDEBUG(D_INODE, "truncate on inode %lu with no objects\n",
96 LASSERT_SEM_LOCKED(&lli->lli_size_sem);
98 if (unlikely((ll_i2sbi(inode)->ll_flags & LL_SBI_CHECKSUM) &&
99 (i_size_read(inode) & ~CFS_PAGE_MASK))) {
100 /* If the truncate leaves a partial page, update its checksum */
101 struct page *page = find_get_page(inode->i_mapping,
102 i_size_read(inode) >>
106 struct ll_async_page *llap = llap_cast_private(page);
108 char *kaddr = kmap_atomic(page, KM_USER0);
109 llap->llap_checksum =
110 init_checksum(OSC_DEFAULT_CKSUM);
111 llap->llap_checksum =
112 compute_checksum(llap->llap_checksum,
113 kaddr, CFS_PAGE_SIZE,
115 kunmap_atomic(kaddr, KM_USER0);
117 page_cache_release(page);
122 new_size = i_size_read(inode);
123 ll_inode_size_unlock(inode, 0);
129 ll_inode_size_unlock(inode, 0);
133 * Finalizes cl-data before exiting typical address_space operation. Dual to
136 static void ll_cl_fini(struct ll_cl_context *lcc)
138 struct lu_env *env = lcc->lcc_env;
139 struct cl_io *io = lcc->lcc_io;
140 struct cl_page *page = lcc->lcc_page;
142 LASSERT(lcc->lcc_cookie == current);
143 LASSERT(env != NULL);
146 lu_ref_del(&page->cp_reference, "cl_io", io);
147 cl_page_put(env, page);
150 if (io && lcc->lcc_created) {
152 cl_io_unlock(env, io);
153 cl_io_iter_fini(env, io);
156 cl_env_put(env, &lcc->lcc_refcheck);
160 * Initializes common cl-data at the typical address_space operation entry
163 static struct ll_cl_context *ll_cl_init(struct file *file,
164 struct page *vmpage, int create)
166 struct ll_cl_context *lcc;
169 struct cl_object *clob;
175 clob = ll_i2info(vmpage->mapping->host)->lli_clob;
176 LASSERT(clob != NULL);
178 env = cl_env_get(&refcheck);
180 return ERR_PTR(PTR_ERR(env));
182 lcc = &vvp_env_info(env)->vti_io_ctx;
183 memset(lcc, 0, sizeof(*lcc));
185 lcc->lcc_refcheck = refcheck;
186 lcc->lcc_cookie = current;
188 cio = ccc_env_io(env);
189 io = cio->cui_cl.cis_io;
190 if (io == NULL && create) {
195 * Loop-back driver calls ->prepare_write() and ->sendfile()
196 * methods directly, bypassing file system ->write() operation,
197 * so cl_io has to be created here.
200 io = &ccc_env_info(env)->cti_io;
201 vio = vvp_env_io(env);
202 ll_io_init(io, file, 1);
204 /* No lock at all for this kind of IO - we can't do it because
205 * we have held page lock, it would cause deadlock.
206 * XXX: This causes poor performance to loop device - One page
208 * In order to get better performance, users should use
209 * lloop driver instead.
211 io->ci_lockreq = CILR_NEVER;
213 pos = (vmpage->index << CFS_PAGE_SHIFT);
215 /* Create a temp IO to serve write. */
216 result = cl_io_rw_init(env, io, CIT_WRITE, pos, CFS_PAGE_SIZE);
218 cio->cui_fd = LUSTRE_FPRIVATE(file);
221 result = cl_io_iter_init(env, io);
223 result = cl_io_lock(env, io);
225 result = cl_io_start(env, io);
228 result = io->ci_result;
229 lcc->lcc_created = 1;
236 struct cl_page *page;
239 LASSERT(io->ci_state == CIS_IO_GOING);
240 LASSERT(cio->cui_fd == LUSTRE_FPRIVATE(file));
241 page = cl_page_find(env, clob, vmpage->index, vmpage,
244 lcc->lcc_page = page;
245 lu_ref_add(&page->cp_reference, "cl_io", io);
248 result = PTR_ERR(page);
252 lcc = ERR_PTR(result);
255 CDEBUG(D_VFSTRACE, "%lu@"DFID" -> %i %p %p\n",
256 vmpage->index, PFID(lu_object_fid(&clob->co_lu)), result,
261 static struct ll_cl_context *ll_cl_get(void)
263 struct ll_cl_context *lcc;
267 env = cl_env_get(&refcheck);
268 LASSERT(!IS_ERR(env));
269 lcc = &vvp_env_info(env)->vti_io_ctx;
270 LASSERT(env == lcc->lcc_env);
271 LASSERT(current == lcc->lcc_cookie);
272 cl_env_put(env, &refcheck);
274 /* env has got in ll_cl_init, so it is still usable. */
279 * ->prepare_write() address space operation called by generic_file_write()
280 * for every page during write.
282 int ll_prepare_write(struct file *file, struct page *vmpage, unsigned from,
285 struct ll_cl_context *lcc;
289 lcc = ll_cl_init(file, vmpage, 1);
291 struct lu_env *env = lcc->lcc_env;
292 struct cl_io *io = lcc->lcc_io;
293 struct cl_page *page = lcc->lcc_page;
295 cl_page_assume(env, io, page);
296 if (cl_io_is_append(io)) {
297 struct cl_object *obj = io->ci_obj;
298 struct inode *inode = ccc_object_inode(obj);
300 * In VFS file->page write loop, for appending, the
301 * write offset might be reset according to the new
302 * file size before holding i_mutex. So crw_pos should
303 * be reset here. BUG:17711.
305 io->u.ci_wr.wr.crw_pos = i_size_read(inode);
307 result = cl_io_prepare_write(env, io, page, from, to);
310 * Add a reference, so that page is not evicted from
311 * the cache until ->commit_write() is called.
314 lu_ref_add(&page->cp_reference, "prepare_write",
317 cl_page_unassume(env, io, page);
320 /* returning 0 in prepare assumes commit must be called
323 result = PTR_ERR(lcc);
328 int ll_commit_write(struct file *file, struct page *vmpage, unsigned from,
331 struct ll_cl_context *lcc;
334 struct cl_page *page;
340 page = lcc->lcc_page;
343 LASSERT(cl_page_is_owned(page, io));
344 result = cl_io_commit_write(env, io, page, from, to);
345 if (cl_page_is_owned(page, io))
346 cl_page_unassume(env, io, page);
348 * Release reference acquired by cl_io_prepare_write().
350 lu_ref_del(&page->cp_reference, "prepare_write", cfs_current());
351 cl_page_put(env, page);
356 struct obd_capa *cl_capa_lookup(struct inode *inode, enum cl_req_type crt)
360 opc = crt == CRT_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
361 return ll_osscapa_get(inode, opc);
364 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
366 /* WARNING: This algorithm is used to reduce the contention on
367 * sbi->ll_lock. It should work well if the ra_max_pages is much
368 * greater than the single file's read-ahead window.
370 * TODO: There may exist a `global sync problem' in this implementation.
371 * Considering the global ra window is 100M, and each file's ra window is 10M,
372 * there are over 10 files trying to get its ra budget and reach
373 * ll_ra_count_get at the exactly same time. All of them will get a zero ra
374 * window, although the global window is 100M. -jay
376 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi, unsigned long len)
378 struct ll_ra_info *ra = &sbi->ll_ra_info;
383 * If read-ahead pages left are less than 1M, do not do read-ahead,
384 * otherwise it will form small read RPC(< 1M), which hurt server
387 ret = min(ra->ra_max_pages - cfs_atomic_read(&ra->ra_cur_pages), len);
388 if ((int)ret < 0 || ret < min((unsigned long)PTLRPC_MAX_BRW_PAGES, len))
391 if (cfs_atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
392 cfs_atomic_sub(ret, &ra->ra_cur_pages);
399 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
401 struct ll_ra_info *ra = &sbi->ll_ra_info;
402 cfs_atomic_sub(len, &ra->ra_cur_pages);
405 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
407 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
408 lprocfs_counter_incr(sbi->ll_ra_stats, which);
411 void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
413 struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
414 ll_ra_stats_inc_sbi(sbi, which);
417 #define RAS_CDEBUG(ras) \
419 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu" \
420 "csr %lu sf %lu sp %lu sl %lu \n", \
421 ras->ras_last_readpage, ras->ras_consecutive_requests, \
422 ras->ras_consecutive_pages, ras->ras_window_start, \
423 ras->ras_window_len, ras->ras_next_readahead, \
424 ras->ras_requests, ras->ras_request_index, \
425 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
426 ras->ras_stride_pages, ras->ras_stride_length)
428 static int index_in_window(unsigned long index, unsigned long point,
429 unsigned long before, unsigned long after)
431 unsigned long start = point - before, end = point + after;
438 return start <= index && index <= end;
441 static struct ll_readahead_state *ll_ras_get(struct file *f)
443 struct ll_file_data *fd;
445 fd = LUSTRE_FPRIVATE(f);
449 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar)
451 struct ll_readahead_state *ras;
455 cfs_spin_lock(&ras->ras_lock);
457 ras->ras_request_index = 0;
458 ras->ras_consecutive_requests++;
459 rar->lrr_reader = current;
461 cfs_list_add(&rar->lrr_linkage, &ras->ras_read_beads);
462 cfs_spin_unlock(&ras->ras_lock);
465 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
467 struct ll_readahead_state *ras;
471 cfs_spin_lock(&ras->ras_lock);
472 cfs_list_del_init(&rar->lrr_linkage);
473 cfs_spin_unlock(&ras->ras_lock);
476 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
478 struct ll_ra_read *scan;
480 cfs_list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
481 if (scan->lrr_reader == current)
487 struct ll_ra_read *ll_ra_read_get(struct file *f)
489 struct ll_readahead_state *ras;
490 struct ll_ra_read *bead;
494 cfs_spin_lock(&ras->ras_lock);
495 bead = ll_ra_read_get_locked(ras);
496 cfs_spin_unlock(&ras->ras_lock);
500 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
501 struct cl_page_list *queue, struct cl_page *page,
510 cl_page_assume(env, io, page);
511 lu_ref_add(&page->cp_reference, "ra", cfs_current());
512 cp = cl2ccc_page(cl_page_at(page, &vvp_device_type));
513 if (!cp->cpg_defer_uptodate && !Page_Uptodate(vmpage)) {
514 rc = cl_page_is_under_lock(env, io, page);
516 cp->cpg_defer_uptodate = 1;
518 cl_page_list_add(queue, page);
521 cl_page_delete(env, page);
525 /* skip completed pages */
526 cl_page_unassume(env, io, page);
527 lu_ref_del(&page->cp_reference, "ra", cfs_current());
528 cl_page_put(env, page);
533 * Initiates read-ahead of a page with given index.
535 * \retval +ve: page was added to \a queue.
537 * \retval -ENOLCK: there is no extent lock for this part of a file, stop
540 * \retval -ve, 0: page wasn't added to \a queue for other reason.
542 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
543 struct cl_page_list *queue,
544 int index, struct address_space *mapping)
547 struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
548 struct cl_page *page;
549 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
550 unsigned int gfp_mask;
552 const char *msg = NULL;
556 gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
558 gfp_mask |= __GFP_NOWARN;
560 vmpage = grab_cache_page_nowait_gfp(mapping, index, gfp_mask);
561 if (vmpage != NULL) {
562 /* Check if vmpage was truncated or reclaimed */
563 if (vmpage->mapping == mapping) {
564 page = cl_page_find(env, clob, vmpage->index,
565 vmpage, CPT_CACHEABLE);
567 rc = cl_read_ahead_page(env, io, queue,
570 which = RA_STAT_FAILED_MATCH;
571 msg = "lock match failed";
574 which = RA_STAT_FAILED_GRAB_PAGE;
575 msg = "cl_page_find failed";
578 which = RA_STAT_WRONG_GRAB_PAGE;
579 msg = "g_c_p_n returned invalid page";
583 page_cache_release(vmpage);
585 which = RA_STAT_FAILED_GRAB_PAGE;
586 msg = "g_c_p_n failed";
589 ll_ra_stats_inc(mapping, which);
590 CDEBUG(D_READA, "%s\n", msg);
595 #define RIA_DEBUG(ria) \
596 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
597 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
600 #define RAS_INCREASE_STEP PTLRPC_MAX_BRW_PAGES
602 static inline int stride_io_mode(struct ll_readahead_state *ras)
604 return ras->ras_consecutive_stride_requests > 1;
606 /* The function calculates how much pages will be read in
607 * [off, off + length], in such stride IO area,
608 * stride_offset = st_off, stride_lengh = st_len,
609 * stride_pages = st_pgs
611 * |------------------|*****|------------------|*****|------------|*****|....
614 * |----- st_len -----|
616 * How many pages it should read in such pattern
617 * |-------------------------------------------------------------|
619 * |<------ length ------->|
621 * = |<----->| + |-------------------------------------| + |---|
622 * start_left st_pgs * i end_left
625 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
626 unsigned long off, unsigned long length)
628 unsigned long start = off > st_off ? off - st_off : 0;
629 unsigned long end = off + length > st_off ? off + length - st_off : 0;
630 unsigned long start_left = 0;
631 unsigned long end_left = 0;
632 unsigned long pg_count;
634 if (st_len == 0 || length == 0 || end == 0)
637 start_left = do_div(start, st_len);
638 if (start_left < st_pgs)
639 start_left = st_pgs - start_left;
643 end_left = do_div(end, st_len);
644 if (end_left > st_pgs)
647 CDEBUG(D_READA, "start %lu, end %lu start_left %lu end_left %lu \n",
648 start, end, start_left, end_left);
651 pg_count = end_left - (st_pgs - start_left);
653 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
655 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
656 "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
661 static int ria_page_count(struct ra_io_arg *ria)
663 __u64 length = ria->ria_end >= ria->ria_start ?
664 ria->ria_end - ria->ria_start + 1 : 0;
666 return stride_pg_count(ria->ria_stoff, ria->ria_length,
667 ria->ria_pages, ria->ria_start,
671 /*Check whether the index is in the defined ra-window */
672 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
674 /* If ria_length == ria_pages, it means non-stride I/O mode,
675 * idx should always inside read-ahead window in this case
676 * For stride I/O mode, just check whether the idx is inside
678 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
679 (idx - ria->ria_stoff) % ria->ria_length < ria->ria_pages;
682 static int ll_read_ahead_pages(const struct lu_env *env,
683 struct cl_io *io, struct cl_page_list *queue,
684 struct ra_io_arg *ria,
685 unsigned long *reserved_pages,
686 struct address_space *mapping,
687 unsigned long *ra_end)
689 int rc, count = 0, stride_ria;
690 unsigned long page_idx;
692 LASSERT(ria != NULL);
695 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
696 for (page_idx = ria->ria_start; page_idx <= ria->ria_end &&
697 *reserved_pages > 0; page_idx++) {
698 if (ras_inside_ra_window(page_idx, ria)) {
699 /* If the page is inside the read-ahead window*/
700 rc = ll_read_ahead_page(env, io, queue,
705 } else if (rc == -ENOLCK)
707 } else if (stride_ria) {
708 /* If it is not in the read-ahead window, and it is
709 * read-ahead mode, then check whether it should skip
712 /* FIXME: This assertion only is valid when it is for
713 * forward read-ahead, it will be fixed when backward
714 * read-ahead is implemented */
715 LASSERTF(page_idx > ria->ria_stoff, "since %lu in the"
716 " gap of ra window,it should bigger than stride"
717 " offset %lu \n", page_idx, ria->ria_stoff);
719 offset = page_idx - ria->ria_stoff;
720 offset = offset % (ria->ria_length);
721 if (offset > ria->ria_pages) {
722 page_idx += ria->ria_length - offset;
723 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
724 ria->ria_length - offset);
733 int ll_readahead(const struct lu_env *env, struct cl_io *io,
734 struct ll_readahead_state *ras, struct address_space *mapping,
735 struct cl_page_list *queue, int flags)
737 struct vvp_io *vio = vvp_env_io(env);
738 struct vvp_thread_info *vti = vvp_env_info(env);
739 struct cl_attr *attr = ccc_env_thread_attr(env);
740 unsigned long start = 0, end = 0, reserved;
741 unsigned long ra_end, len;
743 struct ll_ra_read *bead;
744 struct ra_io_arg *ria = &vti->vti_ria;
745 struct ll_inode_info *lli;
746 struct cl_object *clob;
751 inode = mapping->host;
752 lli = ll_i2info(inode);
753 clob = lli->lli_clob;
755 memset(ria, 0, sizeof *ria);
757 cl_object_attr_lock(clob);
758 ret = cl_object_attr_get(env, clob, attr);
759 cl_object_attr_unlock(clob);
765 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
769 cfs_spin_lock(&ras->ras_lock);
770 if (vio->cui_ra_window_set)
771 bead = &vio->cui_bead;
775 /* Enlarge the RA window to encompass the full read */
776 if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
777 bead->lrr_start + bead->lrr_count) {
778 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
779 ras->ras_window_start;
781 /* Reserve a part of the read-ahead window that we'll be issuing */
782 if (ras->ras_window_len) {
783 start = ras->ras_next_readahead;
784 end = ras->ras_window_start + ras->ras_window_len - 1;
787 /* Truncate RA window to end of file */
788 end = min(end, (unsigned long)((kms - 1) >> CFS_PAGE_SHIFT));
789 ras->ras_next_readahead = max(end, end + 1);
792 ria->ria_start = start;
794 /* If stride I/O mode is detected, get stride window*/
795 if (stride_io_mode(ras)) {
796 ria->ria_stoff = ras->ras_stride_offset;
797 ria->ria_length = ras->ras_stride_length;
798 ria->ria_pages = ras->ras_stride_pages;
800 cfs_spin_unlock(&ras->ras_lock);
803 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
806 len = ria_page_count(ria);
810 reserved = ll_ra_count_get(ll_i2sbi(inode), len);
813 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
815 CDEBUG(D_READA, "reserved page %lu \n", reserved);
817 ret = ll_read_ahead_pages(env, io, queue,
818 ria, &reserved, mapping, &ra_end);
820 LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
822 ll_ra_count_put(ll_i2sbi(inode), reserved);
824 if (ra_end == end + 1 && ra_end == (kms >> CFS_PAGE_SHIFT))
825 ll_ra_stats_inc(mapping, RA_STAT_EOF);
827 /* if we didn't get to the end of the region we reserved from
828 * the ras we need to go back and update the ras so that the
829 * next read-ahead tries from where we left off. we only do so
830 * if the region we failed to issue read-ahead on is still ahead
831 * of the app and behind the next index to start read-ahead from */
832 CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
833 ra_end, end, ria->ria_end);
835 if (ra_end != end + 1) {
836 cfs_spin_lock(&ras->ras_lock);
837 if (ra_end < ras->ras_next_readahead &&
838 index_in_window(ra_end, ras->ras_window_start, 0,
839 ras->ras_window_len)) {
840 ras->ras_next_readahead = ra_end;
843 cfs_spin_unlock(&ras->ras_lock);
849 static void ras_set_start(struct ll_readahead_state *ras, unsigned long index)
851 ras->ras_window_start = index & (~(RAS_INCREASE_STEP - 1));
854 /* called with the ras_lock held or from places where it doesn't matter */
855 static void ras_reset(struct ll_readahead_state *ras, unsigned long index)
857 ras->ras_last_readpage = index;
858 ras->ras_consecutive_requests = 0;
859 ras->ras_consecutive_pages = 0;
860 ras->ras_window_len = 0;
861 ras_set_start(ras, index);
862 ras->ras_next_readahead = max(ras->ras_window_start, index);
867 /* called with the ras_lock held or from places where it doesn't matter */
868 static void ras_stride_reset(struct ll_readahead_state *ras)
870 ras->ras_consecutive_stride_requests = 0;
871 ras->ras_stride_length = 0;
872 ras->ras_stride_pages = 0;
876 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
878 cfs_spin_lock_init(&ras->ras_lock);
880 ras->ras_requests = 0;
881 CFS_INIT_LIST_HEAD(&ras->ras_read_beads);
885 * Check whether the read request is in the stride window.
886 * If it is in the stride window, return 1, otherwise return 0.
888 static int index_in_stride_window(unsigned long index,
889 struct ll_readahead_state *ras,
892 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
894 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
895 ras->ras_stride_pages == ras->ras_stride_length)
898 /* If it is contiguous read */
900 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
902 /*Otherwise check the stride by itself */
903 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
904 ras->ras_consecutive_pages == ras->ras_stride_pages;
907 static void ras_update_stride_detector(struct ll_readahead_state *ras,
910 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
912 if (!stride_io_mode(ras) && (stride_gap != 0 ||
913 ras->ras_consecutive_stride_requests == 0)) {
914 ras->ras_stride_pages = ras->ras_consecutive_pages;
915 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
917 LASSERT(ras->ras_request_index == 0);
918 LASSERT(ras->ras_consecutive_stride_requests == 0);
920 if (index <= ras->ras_last_readpage) {
921 /*Reset stride window for forward read*/
922 ras_stride_reset(ras);
926 ras->ras_stride_pages = ras->ras_consecutive_pages;
927 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
934 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
936 return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
937 ras->ras_stride_pages, ras->ras_stride_offset,
941 /* Stride Read-ahead window will be increased inc_len according to
942 * stride I/O pattern */
943 static void ras_stride_increase_window(struct ll_readahead_state *ras,
944 struct ll_ra_info *ra,
945 unsigned long inc_len)
947 unsigned long left, step, window_len;
948 unsigned long stride_len;
950 LASSERT(ras->ras_stride_length > 0);
951 LASSERTF(ras->ras_window_start + ras->ras_window_len
952 >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
953 " stride_offset %lu\n", ras->ras_window_start,
954 ras->ras_window_len, ras->ras_stride_offset);
956 stride_len = ras->ras_window_start + ras->ras_window_len -
957 ras->ras_stride_offset;
959 left = stride_len % ras->ras_stride_length;
960 window_len = ras->ras_window_len - left;
962 if (left < ras->ras_stride_pages)
965 left = ras->ras_stride_pages + inc_len;
967 LASSERT(ras->ras_stride_pages != 0);
969 step = left / ras->ras_stride_pages;
970 left %= ras->ras_stride_pages;
972 window_len += step * ras->ras_stride_length + left;
974 if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
975 ras->ras_window_len = window_len;
980 static void ras_increase_window(struct ll_readahead_state *ras,
981 struct ll_ra_info *ra, struct inode *inode)
983 /* The stretch of ra-window should be aligned with max rpc_size
984 * but current clio architecture does not support retrieve such
985 * information from lower layer. FIXME later
987 if (stride_io_mode(ras))
988 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP);
990 ras->ras_window_len = min(ras->ras_window_len +
992 ra->ra_max_pages_per_file);
995 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
996 struct ll_readahead_state *ras, unsigned long index,
999 struct ll_ra_info *ra = &sbi->ll_ra_info;
1000 int zero = 0, stride_detect = 0, ra_miss = 0;
1003 cfs_spin_lock(&sbi->ll_lock);
1004 cfs_spin_lock(&ras->ras_lock);
1006 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
1008 /* reset the read-ahead window in two cases. First when the app seeks
1009 * or reads to some other part of the file. Secondly if we get a
1010 * read-ahead miss that we think we've previously issued. This can
1011 * be a symptom of there being so many read-ahead pages that the VM is
1012 * reclaiming it before we get to it. */
1013 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
1015 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
1016 } else if (!hit && ras->ras_window_len &&
1017 index < ras->ras_next_readahead &&
1018 index_in_window(index, ras->ras_window_start, 0,
1019 ras->ras_window_len)) {
1021 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
1024 /* On the second access to a file smaller than the tunable
1025 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
1026 * file up to ra_max_pages_per_file. This is simply a best effort
1027 * and only occurs once per open file. Normal RA behavior is reverted
1028 * to for subsequent IO. The mmap case does not increment
1029 * ras_requests and thus can never trigger this behavior. */
1030 if (ras->ras_requests == 2 && !ras->ras_request_index) {
1033 kms_pages = (i_size_read(inode) + CFS_PAGE_SIZE - 1) >>
1036 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
1037 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
1040 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
1041 ras->ras_window_start = 0;
1042 ras->ras_last_readpage = 0;
1043 ras->ras_next_readahead = 0;
1044 ras->ras_window_len = min(ra->ra_max_pages_per_file,
1045 ra->ra_max_read_ahead_whole_pages);
1046 GOTO(out_unlock, 0);
1050 /* check whether it is in stride I/O mode*/
1051 if (!index_in_stride_window(index, ras, inode)) {
1052 if (ras->ras_consecutive_stride_requests == 0 &&
1053 ras->ras_request_index == 0) {
1054 ras_update_stride_detector(ras, index);
1055 ras->ras_consecutive_stride_requests ++;
1057 ras_stride_reset(ras);
1059 ras_reset(ras, index);
1060 ras->ras_consecutive_pages++;
1061 GOTO(out_unlock, 0);
1063 ras->ras_consecutive_pages = 0;
1064 ras->ras_consecutive_requests = 0;
1065 if (++ras->ras_consecutive_stride_requests > 1)
1071 if (index_in_stride_window(index, ras, inode) &&
1072 stride_io_mode(ras)) {
1073 /*If stride-RA hit cache miss, the stride dector
1074 *will not be reset to avoid the overhead of
1075 *redetecting read-ahead mode */
1076 if (index != ras->ras_last_readpage + 1)
1077 ras->ras_consecutive_pages = 0;
1078 ras_reset(ras, index);
1081 /* Reset both stride window and normal RA
1083 ras_reset(ras, index);
1084 ras->ras_consecutive_pages++;
1085 ras_stride_reset(ras);
1086 GOTO(out_unlock, 0);
1088 } else if (stride_io_mode(ras)) {
1089 /* If this is contiguous read but in stride I/O mode
1090 * currently, check whether stride step still is valid,
1091 * if invalid, it will reset the stride ra window*/
1092 if (!index_in_stride_window(index, ras, inode)) {
1093 /* Shrink stride read-ahead window to be zero */
1094 ras_stride_reset(ras);
1095 ras->ras_window_len = 0;
1096 ras->ras_next_readahead = index;
1100 ras->ras_consecutive_pages++;
1101 ras->ras_last_readpage = index;
1102 ras_set_start(ras, index);
1103 ras->ras_next_readahead = max(ras->ras_window_start,
1104 ras->ras_next_readahead);
1107 /* Trigger RA in the mmap case where ras_consecutive_requests
1108 * is not incremented and thus can't be used to trigger RA */
1109 if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
1110 ras->ras_window_len = RAS_INCREASE_STEP;
1111 GOTO(out_unlock, 0);
1114 /* Initially reset the stride window offset to next_readahead*/
1115 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
1117 * Once stride IO mode is detected, next_readahead should be
1118 * reset to make sure next_readahead > stride offset
1120 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
1121 ras->ras_stride_offset = index;
1122 ras->ras_window_len = RAS_INCREASE_STEP;
1125 /* The initial ras_window_len is set to the request size. To avoid
1126 * uselessly reading and discarding pages for random IO the window is
1127 * only increased once per consecutive request received. */
1128 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
1129 !ras->ras_request_index)
1130 ras_increase_window(ras, ra, inode);
1134 ras->ras_request_index++;
1135 cfs_spin_unlock(&ras->ras_lock);
1136 cfs_spin_unlock(&sbi->ll_lock);
1140 int ll_writepage(struct page *vmpage, struct writeback_control *unused)
1142 struct inode *inode = vmpage->mapping->host;
1145 struct cl_page *page;
1146 struct cl_object *clob;
1147 struct cl_2queue *queue;
1148 struct cl_env_nest nest;
1152 LASSERT(PageLocked(vmpage));
1153 LASSERT(!PageWriteback(vmpage));
1155 if (ll_i2dtexp(inode) == NULL)
1158 env = cl_env_nested_get(&nest);
1160 RETURN(PTR_ERR(env));
1162 io = &ccc_env_info(env)->cti_io;
1163 queue = &vvp_env_info(env)->vti_queue;
1164 clob = ll_i2info(inode)->lli_clob;
1165 LASSERT(clob != NULL);
1168 result = cl_io_init(env, io, CIT_MISC, clob);
1170 page = cl_page_find(env, clob, vmpage->index,
1171 vmpage, CPT_CACHEABLE);
1172 if (!IS_ERR(page)) {
1173 lu_ref_add(&page->cp_reference, "writepage",
1175 cl_page_assume(env, io, page);
1177 * Mark page dirty, because this is what
1178 * ->vio_submit()->cpo_prep_write() assumes.
1180 * XXX better solution is to detect this from within
1181 * cl_io_submit_rw() somehow.
1183 set_page_dirty(vmpage);
1184 cl_2queue_init_page(queue, page);
1185 result = cl_io_submit_rw(env, io, CRT_WRITE,
1187 cl_page_list_disown(env, io, &queue->c2_qin);
1190 * There is no need to clear PG_writeback, as
1191 * cl_io_submit_rw() calls completion callback
1195 * Re-dirty page on error so it retries write,
1196 * but not in case when IO has actually
1197 * occurred and completed with an error.
1199 if (!PageError(vmpage))
1200 set_page_dirty(vmpage);
1202 LASSERT(!cl_page_is_owned(page, io));
1203 lu_ref_del(&page->cp_reference,
1204 "writepage", cfs_current());
1205 cl_page_put(env, page);
1206 cl_2queue_fini(env, queue);
1209 cl_io_fini(env, io);
1210 cl_env_nested_put(&nest, env);
1214 int ll_readpage(struct file *file, struct page *vmpage)
1216 struct ll_cl_context *lcc;
1220 lcc = ll_cl_init(file, vmpage, 0);
1222 struct lu_env *env = lcc->lcc_env;
1223 struct cl_io *io = lcc->lcc_io;
1224 struct cl_page *page = lcc->lcc_page;
1226 LASSERT(page->cp_type == CPT_CACHEABLE);
1227 if (likely(!PageUptodate(vmpage))) {
1228 cl_page_assume(env, io, page);
1229 result = cl_io_read_page(env, io, page);
1231 /* Page from a non-object file. */
1232 LASSERT(!ll_i2info(vmpage->mapping->host)->lli_smd);
1233 unlock_page(vmpage);
1238 result = PTR_ERR(lcc);