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 * Initializes common cl-data at the typical address_space operation entry
136 static int ll_cl_init(struct file *file, struct page *vmpage,
138 struct cl_io **io, struct cl_page **page, int *refcheck)
142 struct cl_page *_page;
143 struct cl_object *clob;
151 clob = ll_i2info(vmpage->mapping->host)->lli_clob;
152 LASSERT(clob != NULL);
154 _env = cl_env_get(refcheck);
156 struct ccc_io *cio = ccc_env_io(_env);
159 *io = _io = cio->cui_cl.cis_io;
161 LASSERT(_io->ci_state == CIS_IO_GOING);
162 LASSERT(cio->cui_fd == LUSTRE_FPRIVATE(file));
163 _page = cl_page_find(_env, clob, vmpage->index, vmpage,
165 if (!IS_ERR(_page)) {
167 lu_ref_add(&_page->cp_reference, "cl_io", _io);
170 result = PTR_ERR(_page);
173 * This is for a case where operation can be called
174 * either with or without cl_io created by the upper
175 * layer (e.g., ->prepare_write() called directly from
180 result = PTR_ERR(_env);
181 CDEBUG(D_VFSTRACE, "%lu@"DFID" -> %i %p %p %p\n",
182 vmpage->index, PFID(lu_object_fid(&clob->co_lu)), result,
188 * Finalizes cl-data before exiting typical address_space operation. Dual to
191 static void ll_cl_fini(struct lu_env *env,
192 struct cl_io *io, struct cl_page *page, int *refcheck)
195 lu_ref_del(&page->cp_reference, "cl_io", io);
196 cl_page_put(env, page);
201 vio = vvp_env_io(env);
202 LASSERT(vio->cui_oneshot >= 0);
203 if (vio->cui_oneshot > 0) {
204 if (--vio->cui_oneshot == 0) {
206 cl_io_unlock(env, io);
207 cl_io_iter_fini(env, io);
209 /* to trigger assertion above, if ll_cl_fini()
210 * is called against freed io. */
211 vio->cui_oneshot = -1;
213 /* additional reference on env was acquired by io,
214 * disable refcheck */
217 cl_env_put(env, refcheck);
223 * Initializes one-shot cl_io for the case when loop driver calls
224 * ->{prepare,commit}_write() methods directly.
226 static int ll_prepare_loop(struct lu_env *env, struct cl_io *io,
227 struct file *file, struct page *vmpage,
228 unsigned from, unsigned to)
235 vio = vvp_env_io(env);
236 cio = ccc_env_io(env);
237 ll_io_init(io, file, 1);
238 pos = (vmpage->index << CFS_PAGE_SHIFT) + from;
240 * Create IO and quickly drive it through CIS_{INIT,IT_STARTED,LOCKED}
241 * states. DLM locks are not taken for vio->cui_oneshot IO---we cannot
242 * take DLM locks here, because page is already locked. With new
243 * ->write_{being,end}() address_space operations lustre might be
246 result = cl_io_rw_init(env, io, CIT_WRITE, pos, from - to);
248 cio->cui_fd = LUSTRE_FPRIVATE(file);
249 vio->cui_oneshot = 1;
250 result = cl_io_iter_init(env, io);
252 result = cl_io_lock(env, io);
254 result = cl_io_start(env, io);
257 result = io->ci_result;
262 * ->prepare_write() address space operation called by generic_file_write()
263 * for every page during write.
265 int ll_prepare_write(struct file *file, struct page *vmpage, unsigned from,
270 struct cl_page *page;
275 result = ll_cl_init(file, vmpage, &env, &io, &page, &refcheck);
277 * Loop-back driver calls ->prepare_write() and ->sendfile() methods
278 * directly, bypassing file system ->write() operation, so cl_io has
279 * to be created here.
281 if (result == -EALREADY) {
282 io = &ccc_env_info(env)->cti_io;
283 result = ll_prepare_loop(env, io, file, vmpage, from, to);
285 result = ll_cl_init(file, vmpage,
286 &env, &io, &page, &refcheck);
287 cl_env_put(env, NULL);
291 cl_page_assume(env, io, page);
292 if (cl_io_is_append(io)) {
293 struct cl_object *obj = io->ci_obj;
294 struct inode *inode = ccc_object_inode(obj);
296 * In VFS file->page write loop, for appending, the
297 * write offset might be reset according to the new
298 * file size before holding i_mutex. So crw_pos should
299 * be reset here. BUG:17711.
301 io->u.ci_wr.wr.crw_pos = i_size_read(inode);
303 result = cl_io_prepare_write(env, io, page, from, to);
308 * Add a reference, so that page is not evicted from
309 * the cache until ->commit_write() is called.
312 lu_ref_add(&page->cp_reference, "prepare_write",
314 vio = vvp_env_io(env);
315 if (vio->cui_oneshot > 0)
318 cl_page_unassume(env, io, page);
320 ll_cl_fini(env, io, page, &refcheck);
324 int ll_commit_write(struct file *file, struct page *vmpage, unsigned from,
329 struct cl_page *page;
334 result = ll_cl_init(file, vmpage, &env, &io, &page, &refcheck);
335 LASSERT(result != -EALREADY);
337 LASSERT(cl_page_is_owned(page, io));
338 result = cl_io_commit_write(env, io, page, from, to);
339 if (cl_page_is_owned(page, io))
340 cl_page_unassume(env, io, page);
342 * Release reference acquired by cl_io_prepare_write().
344 lu_ref_del(&page->cp_reference, "prepare_write", cfs_current());
345 cl_page_put(env, page);
347 ll_cl_fini(env, io, page, &refcheck);
351 struct obd_capa *cl_capa_lookup(struct inode *inode, enum cl_req_type crt)
355 opc = crt == CRT_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
356 return ll_osscapa_get(inode, opc);
359 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
361 /* WARNING: This algorithm is used to reduce the contention on
362 * sbi->ll_lock. It should work well if the ra_max_pages is much
363 * greater than the single file's read-ahead window.
365 * TODO: There may exist a `global sync problem' in this implementation.
366 * Considering the global ra window is 100M, and each file's ra window is 10M,
367 * there are over 10 files trying to get its ra budget and reach
368 * ll_ra_count_get at the exactly same time. All of them will get a zero ra
369 * window, although the global window is 100M. -jay
371 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi, unsigned long len)
373 struct ll_ra_info *ra = &sbi->ll_ra_info;
377 ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages), len);
381 if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
382 atomic_sub(ret, &ra->ra_cur_pages);
389 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
391 struct ll_ra_info *ra = &sbi->ll_ra_info;
392 atomic_sub(len, &ra->ra_cur_pages);
395 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
397 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
398 lprocfs_counter_incr(sbi->ll_ra_stats, which);
401 void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
403 struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
404 ll_ra_stats_inc_sbi(sbi, which);
407 #define RAS_CDEBUG(ras) \
409 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu" \
410 "csr %lu sf %lu sp %lu sl %lu \n", \
411 ras->ras_last_readpage, ras->ras_consecutive_requests, \
412 ras->ras_consecutive_pages, ras->ras_window_start, \
413 ras->ras_window_len, ras->ras_next_readahead, \
414 ras->ras_requests, ras->ras_request_index, \
415 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
416 ras->ras_stride_pages, ras->ras_stride_length)
418 static int index_in_window(unsigned long index, unsigned long point,
419 unsigned long before, unsigned long after)
421 unsigned long start = point - before, end = point + after;
428 return start <= index && index <= end;
431 static struct ll_readahead_state *ll_ras_get(struct file *f)
433 struct ll_file_data *fd;
435 fd = LUSTRE_FPRIVATE(f);
439 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar)
441 struct ll_readahead_state *ras;
445 spin_lock(&ras->ras_lock);
447 ras->ras_request_index = 0;
448 ras->ras_consecutive_requests++;
449 rar->lrr_reader = current;
451 list_add(&rar->lrr_linkage, &ras->ras_read_beads);
452 spin_unlock(&ras->ras_lock);
455 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
457 struct ll_readahead_state *ras;
461 spin_lock(&ras->ras_lock);
462 list_del_init(&rar->lrr_linkage);
463 spin_unlock(&ras->ras_lock);
466 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
468 struct ll_ra_read *scan;
470 list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
471 if (scan->lrr_reader == current)
477 struct ll_ra_read *ll_ra_read_get(struct file *f)
479 struct ll_readahead_state *ras;
480 struct ll_ra_read *bead;
484 spin_lock(&ras->ras_lock);
485 bead = ll_ra_read_get_locked(ras);
486 spin_unlock(&ras->ras_lock);
490 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
491 struct cl_page_list *queue, struct cl_page *page,
500 cl_page_assume(env, io, page);
501 lu_ref_add(&page->cp_reference, "ra", cfs_current());
502 cp = cl2ccc_page(cl_page_at(page, &vvp_device_type));
503 if (!cp->cpg_defer_uptodate && !Page_Uptodate(vmpage)) {
504 rc = cl_page_is_under_lock(env, io, page);
506 cp->cpg_defer_uptodate = 1;
508 cl_page_list_add(queue, page);
511 cl_page_delete(env, page);
515 /* skip completed pages */
516 cl_page_unassume(env, io, page);
517 lu_ref_del(&page->cp_reference, "ra", cfs_current());
518 cl_page_put(env, page);
523 * Initiates read-ahead of a page with given index.
525 * \retval +ve: page was added to \a queue.
527 * \retval -ENOLCK: there is no extent lock for this part of a file, stop
530 * \retval -ve, 0: page wasn't added to \a queue for other reason.
532 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
533 struct cl_page_list *queue,
534 int index, struct address_space *mapping)
537 struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
538 struct cl_page *page;
539 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
540 unsigned int gfp_mask;
542 const char *msg = NULL;
546 gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
548 gfp_mask |= __GFP_NOWARN;
550 vmpage = grab_cache_page_nowait_gfp(mapping, index, gfp_mask);
551 if (vmpage != NULL) {
552 /* Check if vmpage was truncated or reclaimed */
553 if (vmpage->mapping == mapping) {
554 page = cl_page_find(env, clob, vmpage->index,
555 vmpage, CPT_CACHEABLE);
557 rc = cl_read_ahead_page(env, io, queue,
560 which = RA_STAT_FAILED_MATCH;
561 msg = "lock match failed";
564 which = RA_STAT_FAILED_GRAB_PAGE;
565 msg = "cl_page_find failed";
568 which = RA_STAT_WRONG_GRAB_PAGE;
569 msg = "g_c_p_n returned invalid page";
573 page_cache_release(vmpage);
575 which = RA_STAT_FAILED_GRAB_PAGE;
576 msg = "g_c_p_n failed";
579 ll_ra_stats_inc(mapping, which);
580 CDEBUG(D_READA, "%s\n", msg);
585 #define RIA_DEBUG(ria) \
586 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
587 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
590 #define RAS_INCREASE_STEP (1024 * 1024 >> CFS_PAGE_SHIFT)
592 static inline int stride_io_mode(struct ll_readahead_state *ras)
594 return ras->ras_consecutive_stride_requests > 1;
597 /* The function calculates how much pages will be read in
598 * [off, off + length], which will be read by stride I/O mode,
599 * stride_offset = st_off, stride_lengh = st_len,
600 * stride_pages = st_pgs
603 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
604 unsigned long off, unsigned length)
606 unsigned long cont_len = st_off > off ? st_off - off : 0;
607 __u64 stride_len = length + off > st_off ?
608 length + off + 1 - st_off : 0;
609 unsigned long left, pg_count;
611 if (st_len == 0 || length == 0)
614 left = do_div(stride_len, st_len);
615 left = min(left, st_pgs);
617 pg_count = left + stride_len * st_pgs + cont_len;
619 LASSERT(pg_count >= left);
621 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %u"
622 "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
627 static int ria_page_count(struct ra_io_arg *ria)
629 __u64 length = ria->ria_end >= ria->ria_start ?
630 ria->ria_end - ria->ria_start + 1 : 0;
632 return stride_pg_count(ria->ria_stoff, ria->ria_length,
633 ria->ria_pages, ria->ria_start,
637 /*Check whether the index is in the defined ra-window */
638 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
640 /* If ria_length == ria_pages, it means non-stride I/O mode,
641 * idx should always inside read-ahead window in this case
642 * For stride I/O mode, just check whether the idx is inside
644 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
645 (idx - ria->ria_stoff) % 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, "since %lu in the"
682 " gap of ra window,it should bigger than stride"
683 " offset %lu \n", page_idx, ria->ria_stoff);
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 ccc_thread_info *cti = ccc_env_info(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;
713 struct cl_attr *attr = &cti->cti_attr;
718 inode = mapping->host;
719 lli = ll_i2info(inode);
720 clob = lli->lli_clob;
722 memset(ria, 0, sizeof *ria);
724 cl_object_attr_lock(clob);
725 ret = cl_object_attr_get(env, clob, attr);
726 cl_object_attr_unlock(clob);
732 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
736 spin_lock(&ras->ras_lock);
737 if (vio->cui_ra_window_set)
738 bead = &vio->cui_bead;
742 /* Enlarge the RA window to encompass the full read */
743 if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
744 bead->lrr_start + bead->lrr_count) {
745 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
746 ras->ras_window_start;
748 /* Reserve a part of the read-ahead window that we'll be issuing */
749 if (ras->ras_window_len) {
750 start = ras->ras_next_readahead;
751 end = ras->ras_window_start + ras->ras_window_len - 1;
754 /* Truncate RA window to end of file */
755 end = min(end, (unsigned long)((kms - 1) >> CFS_PAGE_SHIFT));
756 ras->ras_next_readahead = max(end, end + 1);
759 ria->ria_start = start;
761 /* If stride I/O mode is detected, get stride window*/
762 if (stride_io_mode(ras)) {
763 ria->ria_stoff = ras->ras_stride_offset;
764 ria->ria_length = ras->ras_stride_length;
765 ria->ria_pages = ras->ras_stride_pages;
767 spin_unlock(&ras->ras_lock);
770 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
773 len = ria_page_count(ria);
777 reserved = ll_ra_count_get(ll_i2sbi(inode), len);
780 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
782 CDEBUG(D_READA, "reserved page %lu \n", reserved);
784 ret = ll_read_ahead_pages(env, io, queue,
785 ria, &reserved, mapping, &ra_end);
787 LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
789 ll_ra_count_put(ll_i2sbi(inode), reserved);
791 if (ra_end == end + 1 && ra_end == (kms >> CFS_PAGE_SHIFT))
792 ll_ra_stats_inc(mapping, RA_STAT_EOF);
794 /* if we didn't get to the end of the region we reserved from
795 * the ras we need to go back and update the ras so that the
796 * next read-ahead tries from where we left off. we only do so
797 * if the region we failed to issue read-ahead on is still ahead
798 * of the app and behind the next index to start read-ahead from */
799 CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
800 ra_end, end, ria->ria_end);
802 if (ra_end != end + 1) {
803 spin_lock(&ras->ras_lock);
804 if (ra_end < ras->ras_next_readahead &&
805 index_in_window(ra_end, ras->ras_window_start, 0,
806 ras->ras_window_len)) {
807 ras->ras_next_readahead = ra_end;
810 spin_unlock(&ras->ras_lock);
816 static void ras_set_start(struct ll_readahead_state *ras, unsigned long index)
818 ras->ras_window_start = index & (~(RAS_INCREASE_STEP - 1));
821 /* called with the ras_lock held or from places where it doesn't matter */
822 static void ras_reset(struct ll_readahead_state *ras, unsigned long index)
824 ras->ras_last_readpage = index;
825 ras->ras_consecutive_requests = 0;
826 ras->ras_consecutive_pages = 0;
827 ras->ras_window_len = 0;
828 ras_set_start(ras, index);
829 ras->ras_next_readahead = max(ras->ras_window_start, index);
834 /* called with the ras_lock held or from places where it doesn't matter */
835 static void ras_stride_reset(struct ll_readahead_state *ras)
837 ras->ras_consecutive_stride_requests = 0;
838 ras->ras_stride_length = 0;
839 ras->ras_stride_pages = 0;
843 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
845 spin_lock_init(&ras->ras_lock);
847 ras->ras_requests = 0;
848 INIT_LIST_HEAD(&ras->ras_read_beads);
852 * Check whether the read request is in the stride window.
853 * If it is in the stride window, return 1, otherwise return 0.
855 static int index_in_stride_window(unsigned long index,
856 struct ll_readahead_state *ras,
859 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
861 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0)
864 /* If it is contiguous read */
866 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
868 /* Otherwise check the stride by itself */
869 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
870 ras->ras_consecutive_pages == ras->ras_stride_pages;
873 static void ras_update_stride_detector(struct ll_readahead_state *ras,
876 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
878 if (!stride_io_mode(ras) && (stride_gap != 0 ||
879 ras->ras_consecutive_stride_requests == 0)) {
880 ras->ras_stride_pages = ras->ras_consecutive_pages;
881 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
887 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
889 return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
890 ras->ras_stride_pages, ras->ras_stride_offset,
894 /* Stride Read-ahead window will be increased inc_len according to
895 * stride I/O pattern */
896 static void ras_stride_increase_window(struct ll_readahead_state *ras,
897 struct ll_ra_info *ra,
898 unsigned long inc_len)
900 unsigned long left, step, window_len;
901 unsigned long stride_len;
903 LASSERT(ras->ras_stride_length > 0);
904 LASSERTF(ras->ras_window_start + ras->ras_window_len
905 >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
906 " stride_offset %lu\n", ras->ras_window_start,
907 ras->ras_window_len, ras->ras_stride_offset);
909 stride_len = ras->ras_window_start + ras->ras_window_len -
910 ras->ras_stride_offset;
912 left = stride_len % ras->ras_stride_length;
913 window_len = ras->ras_window_len - left;
915 if (left < ras->ras_stride_pages)
918 left = ras->ras_stride_pages + inc_len;
920 LASSERT(ras->ras_stride_pages != 0);
922 step = left / ras->ras_stride_pages;
923 left %= ras->ras_stride_pages;
925 window_len += step * ras->ras_stride_length + left;
927 if (stride_page_count(ras, window_len) <= ra->ra_max_pages)
928 ras->ras_window_len = window_len;
933 /* Set stride I/O read-ahead window start offset */
934 static void ras_set_stride_offset(struct ll_readahead_state *ras)
936 unsigned long window_len = ras->ras_next_readahead -
937 ras->ras_window_start;
940 LASSERT(ras->ras_stride_length != 0);
942 left = window_len % ras->ras_stride_length;
944 ras->ras_stride_offset = ras->ras_next_readahead - left;
949 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
950 struct ll_readahead_state *ras, unsigned long index,
953 struct ll_ra_info *ra = &sbi->ll_ra_info;
954 int zero = 0, stride_detect = 0, ra_miss = 0;
957 spin_lock(&sbi->ll_lock);
958 spin_lock(&ras->ras_lock);
960 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
962 /* reset the read-ahead window in two cases. First when the app seeks
963 * or reads to some other part of the file. Secondly if we get a
964 * read-ahead miss that we think we've previously issued. This can
965 * be a symptom of there being so many read-ahead pages that the VM is
966 * reclaiming it before we get to it. */
967 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
969 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
970 } else if (!hit && ras->ras_window_len &&
971 index < ras->ras_next_readahead &&
972 index_in_window(index, ras->ras_window_start, 0,
973 ras->ras_window_len)) {
975 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
978 /* On the second access to a file smaller than the tunable
979 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
980 * file up to ra_max_pages. This is simply a best effort and
981 * only occurs once per open file. Normal RA behavior is reverted
982 * to for subsequent IO. The mmap case does not increment
983 * ras_requests and thus can never trigger this behavior. */
984 if (ras->ras_requests == 2 && !ras->ras_request_index) {
987 kms_pages = (i_size_read(inode) + CFS_PAGE_SIZE - 1) >>
990 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
991 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages);
994 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
995 ras->ras_window_start = 0;
996 ras->ras_last_readpage = 0;
997 ras->ras_next_readahead = 0;
998 ras->ras_window_len = min(ra->ra_max_pages,
999 ra->ra_max_read_ahead_whole_pages);
1000 GOTO(out_unlock, 0);
1004 /* check whether it is in stride I/O mode*/
1005 if (!index_in_stride_window(index, ras, inode)) {
1006 ras_reset(ras, index);
1007 ras->ras_consecutive_pages++;
1008 ras_stride_reset(ras);
1009 GOTO(out_unlock, 0);
1011 ras->ras_consecutive_requests = 0;
1012 if (++ras->ras_consecutive_stride_requests > 1)
1018 if (index_in_stride_window(index, ras, inode) &&
1019 stride_io_mode(ras)) {
1020 /*If stride-RA hit cache miss, the stride dector
1021 *will not be reset to avoid the overhead of
1022 *redetecting read-ahead mode */
1023 if (index != ras->ras_last_readpage + 1)
1024 ras->ras_consecutive_pages = 0;
1027 /* Reset both stride window and normal RA window */
1028 ras_reset(ras, index);
1029 ras->ras_consecutive_pages++;
1030 ras_stride_reset(ras);
1031 GOTO(out_unlock, 0);
1033 } else if (stride_io_mode(ras)) {
1034 /* If this is contiguous read but in stride I/O mode
1035 * currently, check whether stride step still is valid,
1036 * if invalid, it will reset the stride ra window*/
1037 if (!index_in_stride_window(index, ras, inode)) {
1038 /* Shrink stride read-ahead window to be zero */
1039 ras_stride_reset(ras);
1040 ras->ras_window_len = 0;
1041 ras->ras_next_readahead = index;
1045 ras->ras_consecutive_pages++;
1046 ras_update_stride_detector(ras, index);
1047 ras->ras_last_readpage = index;
1048 ras_set_start(ras, index);
1049 ras->ras_next_readahead = max(ras->ras_window_start,
1050 ras->ras_next_readahead);
1053 /* Trigger RA in the mmap case where ras_consecutive_requests
1054 * is not incremented and thus can't be used to trigger RA */
1055 if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
1056 ras->ras_window_len = RAS_INCREASE_STEP;
1057 GOTO(out_unlock, 0);
1060 /* Initially reset the stride window offset to next_readahead*/
1061 if (ras->ras_consecutive_stride_requests == 2 && stride_detect)
1062 ras_set_stride_offset(ras);
1064 /* The initial ras_window_len is set to the request size. To avoid
1065 * uselessly reading and discarding pages for random IO the window is
1066 * only increased once per consecutive request received. */
1067 if ((ras->ras_consecutive_requests > 1 &&
1068 !ras->ras_request_index) || stride_detect) {
1069 if (stride_io_mode(ras))
1070 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP);
1072 ras->ras_window_len = min(ras->ras_window_len +
1079 ras->ras_request_index++;
1080 spin_unlock(&ras->ras_lock);
1081 spin_unlock(&sbi->ll_lock);
1085 int ll_writepage(struct page *vmpage, struct writeback_control *_)
1087 struct inode *inode = vmpage->mapping->host;
1090 struct cl_page *page;
1091 struct cl_object *clob;
1092 struct cl_2queue *queue;
1093 struct cl_env_nest nest;
1097 LASSERT(PageLocked(vmpage));
1098 LASSERT(!PageWriteback(vmpage));
1100 if (ll_i2dtexp(inode) == NULL)
1103 env = cl_env_nested_get(&nest);
1105 RETURN(PTR_ERR(env));
1107 io = &ccc_env_info(env)->cti_io;
1108 queue = &vvp_env_info(env)->vti_queue;
1109 clob = ll_i2info(inode)->lli_clob;
1110 LASSERT(clob != NULL);
1113 result = cl_io_init(env, io, CIT_MISC, clob);
1115 page = cl_page_find(env, clob, vmpage->index,
1116 vmpage, CPT_CACHEABLE);
1117 if (!IS_ERR(page)) {
1118 lu_ref_add(&page->cp_reference, "writepage",
1120 cl_page_assume(env, io, page);
1122 * Mark page dirty, because this is what
1123 * ->vio_submit()->cpo_prep_write() assumes.
1125 * XXX better solution is to detect this from within
1126 * cl_io_submit_rw() somehow.
1128 set_page_dirty(vmpage);
1129 cl_2queue_init_page(queue, page);
1130 result = cl_io_submit_rw(env, io, CRT_WRITE,
1132 cl_page_list_disown(env, io, &queue->c2_qin);
1135 * There is no need to clear PG_writeback, as
1136 * cl_io_submit_rw() calls completion callback
1140 * Re-dirty page on error so it retries write,
1141 * but not in case when IO has actually
1142 * occurred and completed with an error.
1144 if (!PageError(vmpage))
1145 set_page_dirty(vmpage);
1147 LASSERT(!cl_page_is_owned(page, io));
1148 lu_ref_del(&page->cp_reference,
1149 "writepage", cfs_current());
1150 cl_page_put(env, page);
1151 cl_2queue_fini(env, queue);
1154 cl_io_fini(env, io);
1155 cl_env_nested_put(&nest, env);
1159 int ll_readpage(struct file *file, struct page *vmpage)
1163 struct cl_page *page;
1168 result = ll_cl_init(file, vmpage, &env, &io, &page, &refcheck);
1170 LASSERT(page->cp_type == CPT_CACHEABLE);
1171 if (likely(!PageUptodate(vmpage))) {
1172 cl_page_assume(env, io, page);
1173 result = cl_io_read_page(env, io, page);
1175 /* Page from a non-object file. */
1176 LASSERT(!ll_i2info(vmpage->mapping->host)->lli_smd);
1177 unlock_page(vmpage);
1181 LASSERT(!cl_page_is_owned(page, io));
1182 ll_cl_fini(env, io, page, &refcheck);