4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Lustre Lite I/O page cache routines shared by different kernel revs
41 #include <linux/kernel.h>
43 #include <linux/string.h>
44 #include <linux/stat.h>
45 #include <linux/errno.h>
46 #include <linux/smp_lock.h>
47 #include <linux/unistd.h>
48 #include <linux/version.h>
49 #include <asm/system.h>
50 #include <asm/uaccess.h>
53 #include <linux/stat.h>
54 #include <asm/uaccess.h>
56 #include <linux/pagemap.h>
57 #include <linux/smp_lock.h>
58 /* current_is_kswapd() */
59 #include <linux/swap.h>
61 #define DEBUG_SUBSYSTEM S_LLITE
63 #include <lustre_lite.h>
64 #include <obd_cksum.h>
65 #include "llite_internal.h"
66 #include <linux/lustre_compat25.h>
68 /* this isn't where truncate starts. roughly:
69 * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate. setattr_raw grabs
70 * DLM lock on [size, EOF], i_mutex, ->lli_size_sem, and WRITE_I_ALLOC_SEM to
73 * must be called under ->lli_size_sem */
74 void ll_truncate(struct inode *inode)
78 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) to %llu\n", inode->i_ino,
79 inode->i_generation, inode, i_size_read(inode));
86 * Finalizes cl-data before exiting typical address_space operation. Dual to
89 static void ll_cl_fini(struct ll_cl_context *lcc)
91 struct lu_env *env = lcc->lcc_env;
92 struct cl_io *io = lcc->lcc_io;
93 struct cl_page *page = lcc->lcc_page;
95 LASSERT(lcc->lcc_cookie == current);
99 lu_ref_del(&page->cp_reference, "cl_io", io);
100 cl_page_put(env, page);
103 if (io && lcc->lcc_created) {
105 cl_io_unlock(env, io);
106 cl_io_iter_fini(env, io);
109 cl_env_put(env, &lcc->lcc_refcheck);
113 * Initializes common cl-data at the typical address_space operation entry
116 static struct ll_cl_context *ll_cl_init(struct file *file,
117 struct page *vmpage, int create)
119 struct ll_cl_context *lcc;
122 struct cl_object *clob;
128 clob = ll_i2info(vmpage->mapping->host)->lli_clob;
129 LASSERT(clob != NULL);
131 env = cl_env_get(&refcheck);
133 return ERR_PTR(PTR_ERR(env));
135 lcc = &vvp_env_info(env)->vti_io_ctx;
136 memset(lcc, 0, sizeof(*lcc));
138 lcc->lcc_refcheck = refcheck;
139 lcc->lcc_cookie = current;
141 cio = ccc_env_io(env);
142 io = cio->cui_cl.cis_io;
143 if (io == NULL && create) {
147 * Loop-back driver calls ->prepare_write() and ->sendfile()
148 * methods directly, bypassing file system ->write() operation,
149 * so cl_io has to be created here.
151 io = ccc_env_thread_io(env);
152 ll_io_init(io, file, 1);
154 /* No lock at all for this kind of IO - we can't do it because
155 * we have held page lock, it would cause deadlock.
156 * XXX: This causes poor performance to loop device - One page
158 * In order to get better performance, users should use
159 * lloop driver instead.
161 io->ci_lockreq = CILR_NEVER;
163 pos = (vmpage->index << CFS_PAGE_SHIFT);
165 /* Create a temp IO to serve write. */
166 result = cl_io_rw_init(env, io, CIT_WRITE, pos, CFS_PAGE_SIZE);
168 cio->cui_fd = LUSTRE_FPRIVATE(file);
171 result = cl_io_iter_init(env, io);
173 result = cl_io_lock(env, io);
175 result = cl_io_start(env, io);
178 result = io->ci_result;
179 lcc->lcc_created = 1;
186 struct cl_page *page;
189 LASSERT(io->ci_state == CIS_IO_GOING);
190 LASSERT(cio->cui_fd == LUSTRE_FPRIVATE(file));
191 page = cl_page_find(env, clob, vmpage->index, vmpage,
194 lcc->lcc_page = page;
195 lu_ref_add(&page->cp_reference, "cl_io", io);
198 result = PTR_ERR(page);
202 lcc = ERR_PTR(result);
205 CDEBUG(D_VFSTRACE, "%lu@"DFID" -> %d %p %p\n",
206 vmpage->index, PFID(lu_object_fid(&clob->co_lu)), result,
211 static struct ll_cl_context *ll_cl_get(void)
213 struct ll_cl_context *lcc;
217 env = cl_env_get(&refcheck);
218 LASSERT(!IS_ERR(env));
219 lcc = &vvp_env_info(env)->vti_io_ctx;
220 LASSERT(env == lcc->lcc_env);
221 LASSERT(current == lcc->lcc_cookie);
222 cl_env_put(env, &refcheck);
224 /* env has got in ll_cl_init, so it is still usable. */
229 * ->prepare_write() address space operation called by generic_file_write()
230 * for every page during write.
232 int ll_prepare_write(struct file *file, struct page *vmpage, unsigned from,
235 struct ll_cl_context *lcc;
239 lcc = ll_cl_init(file, vmpage, 1);
241 struct lu_env *env = lcc->lcc_env;
242 struct cl_io *io = lcc->lcc_io;
243 struct cl_page *page = lcc->lcc_page;
245 cl_page_assume(env, io, page);
246 if (cl_io_is_append(io)) {
247 struct cl_object *obj = io->ci_obj;
248 struct inode *inode = ccc_object_inode(obj);
250 * In VFS file->page write loop, for appending, the
251 * write offset might be reset according to the new
252 * file size before holding i_mutex. So crw_pos should
253 * be reset here. BUG:17711.
255 io->u.ci_wr.wr.crw_pos = i_size_read(inode);
257 result = cl_io_prepare_write(env, io, page, from, to);
260 * Add a reference, so that page is not evicted from
261 * the cache until ->commit_write() is called.
264 lu_ref_add(&page->cp_reference, "prepare_write",
267 cl_page_unassume(env, io, page);
270 /* returning 0 in prepare assumes commit must be called
273 result = PTR_ERR(lcc);
278 int ll_commit_write(struct file *file, struct page *vmpage, unsigned from,
281 struct ll_cl_context *lcc;
284 struct cl_page *page;
290 page = lcc->lcc_page;
293 LASSERT(cl_page_is_owned(page, io));
295 if (from != to) /* handle short write case. */
296 result = cl_io_commit_write(env, io, page, from, to);
297 if (cl_page_is_owned(page, io))
298 cl_page_unassume(env, io, page);
301 * Release reference acquired by ll_prepare_write().
303 lu_ref_del(&page->cp_reference, "prepare_write", cfs_current());
304 cl_page_put(env, page);
309 struct obd_capa *cl_capa_lookup(struct inode *inode, enum cl_req_type crt)
313 opc = crt == CRT_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
314 return ll_osscapa_get(inode, opc);
317 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
320 * Get readahead pages from the filesystem readahead pool of the client for a
323 * /param sbi superblock for filesystem readahead state ll_ra_info
324 * /param ria per-thread readahead state
325 * /param pages number of pages requested for readahead for the thread.
327 * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
328 * It should work well if the ra_max_pages is much greater than the single
329 * file's read-ahead window, and not too many threads contending for
330 * these readahead pages.
332 * TODO: There may be a 'global sync problem' if many threads are trying
333 * to get an ra budget that is larger than the remaining readahead pages
334 * and reach here at exactly the same time. They will compute /a ret to
335 * consume the remaining pages, but will fail at atomic_add_return() and
336 * get a zero ra window, although there is still ra space remaining. - Jay */
338 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
339 struct ra_io_arg *ria,
342 struct ll_ra_info *ra = &sbi->ll_ra_info;
346 /* If read-ahead pages left are less than 1M, do not do read-ahead,
347 * otherwise it will form small read RPC(< 1M), which hurt server
348 * performance a lot. */
349 ret = min(ra->ra_max_pages - cfs_atomic_read(&ra->ra_cur_pages), pages);
350 if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages))
353 /* If the non-strided (ria_pages == 0) readahead window
354 * (ria_start + ret) has grown across an RPC boundary, then trim
355 * readahead size by the amount beyond the RPC so it ends on an
356 * RPC boundary. If the readahead window is already ending on
357 * an RPC boundary (beyond_rpc == 0), or smaller than a full
358 * RPC (beyond_rpc < ret) the readahead size is unchanged.
359 * The (beyond_rpc != 0) check is skipped since the conditional
360 * branch is more expensive than subtracting zero from the result.
362 * Strided read is left unaligned to avoid small fragments beyond
363 * the RPC boundary from needing an extra read RPC. */
364 if (ria->ria_pages == 0) {
365 long beyond_rpc = (ria->ria_start + ret) % PTLRPC_MAX_BRW_PAGES;
366 if (/* beyond_rpc != 0 && */ beyond_rpc < ret)
370 if (cfs_atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
371 cfs_atomic_sub(ret, &ra->ra_cur_pages);
379 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
381 struct ll_ra_info *ra = &sbi->ll_ra_info;
382 cfs_atomic_sub(len, &ra->ra_cur_pages);
385 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
387 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
388 lprocfs_counter_incr(sbi->ll_ra_stats, which);
391 void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
393 struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
394 ll_ra_stats_inc_sbi(sbi, which);
397 #define RAS_CDEBUG(ras) \
399 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu" \
400 "csr %lu sf %lu sp %lu sl %lu \n", \
401 ras->ras_last_readpage, ras->ras_consecutive_requests, \
402 ras->ras_consecutive_pages, ras->ras_window_start, \
403 ras->ras_window_len, ras->ras_next_readahead, \
404 ras->ras_requests, ras->ras_request_index, \
405 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
406 ras->ras_stride_pages, ras->ras_stride_length)
408 static int index_in_window(unsigned long index, unsigned long point,
409 unsigned long before, unsigned long after)
411 unsigned long start = point - before, end = point + after;
418 return start <= index && index <= end;
421 static struct ll_readahead_state *ll_ras_get(struct file *f)
423 struct ll_file_data *fd;
425 fd = LUSTRE_FPRIVATE(f);
429 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar)
431 struct ll_readahead_state *ras;
435 cfs_spin_lock(&ras->ras_lock);
437 ras->ras_request_index = 0;
438 ras->ras_consecutive_requests++;
439 rar->lrr_reader = current;
441 cfs_list_add(&rar->lrr_linkage, &ras->ras_read_beads);
442 cfs_spin_unlock(&ras->ras_lock);
445 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
447 struct ll_readahead_state *ras;
451 cfs_spin_lock(&ras->ras_lock);
452 cfs_list_del_init(&rar->lrr_linkage);
453 cfs_spin_unlock(&ras->ras_lock);
456 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
458 struct ll_ra_read *scan;
460 cfs_list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
461 if (scan->lrr_reader == current)
467 struct ll_ra_read *ll_ra_read_get(struct file *f)
469 struct ll_readahead_state *ras;
470 struct ll_ra_read *bead;
474 cfs_spin_lock(&ras->ras_lock);
475 bead = ll_ra_read_get_locked(ras);
476 cfs_spin_unlock(&ras->ras_lock);
480 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
481 struct cl_page_list *queue, struct cl_page *page,
490 cl_page_assume(env, io, page);
491 lu_ref_add(&page->cp_reference, "ra", cfs_current());
492 cp = cl2ccc_page(cl_page_at(page, &vvp_device_type));
493 if (!cp->cpg_defer_uptodate && !Page_Uptodate(vmpage)) {
494 rc = cl_page_is_under_lock(env, io, page);
496 cp->cpg_defer_uptodate = 1;
498 cl_page_list_add(queue, page);
501 cl_page_delete(env, page);
505 /* skip completed pages */
506 cl_page_unassume(env, io, page);
507 lu_ref_del(&page->cp_reference, "ra", cfs_current());
508 cl_page_put(env, page);
513 * Initiates read-ahead of a page with given index.
515 * \retval +ve: page was added to \a queue.
517 * \retval -ENOLCK: there is no extent lock for this part of a file, stop
520 * \retval -ve, 0: page wasn't added to \a queue for other reason.
522 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
523 struct cl_page_list *queue,
524 pgoff_t index, struct address_space *mapping)
527 struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
528 struct cl_page *page;
529 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
530 unsigned int gfp_mask;
532 const char *msg = NULL;
536 gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
538 gfp_mask |= __GFP_NOWARN;
540 vmpage = grab_cache_page_nowait(mapping, index);
541 if (vmpage != NULL) {
542 /* Check if vmpage was truncated or reclaimed */
543 if (vmpage->mapping == mapping) {
544 page = cl_page_find(env, clob, vmpage->index,
545 vmpage, CPT_CACHEABLE);
547 rc = cl_read_ahead_page(env, io, queue,
550 which = RA_STAT_FAILED_MATCH;
551 msg = "lock match failed";
554 which = RA_STAT_FAILED_GRAB_PAGE;
555 msg = "cl_page_find failed";
558 which = RA_STAT_WRONG_GRAB_PAGE;
559 msg = "g_c_p_n returned invalid page";
563 page_cache_release(vmpage);
565 which = RA_STAT_FAILED_GRAB_PAGE;
566 msg = "g_c_p_n failed";
569 ll_ra_stats_inc(mapping, which);
570 CDEBUG(D_READA, "%s\n", msg);
575 #define RIA_DEBUG(ria) \
576 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
577 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
580 #define RAS_INCREASE_STEP PTLRPC_MAX_BRW_PAGES
582 static inline int stride_io_mode(struct ll_readahead_state *ras)
584 return ras->ras_consecutive_stride_requests > 1;
586 /* The function calculates how much pages will be read in
587 * [off, off + length], in such stride IO area,
588 * stride_offset = st_off, stride_lengh = st_len,
589 * stride_pages = st_pgs
591 * |------------------|*****|------------------|*****|------------|*****|....
594 * |----- st_len -----|
596 * How many pages it should read in such pattern
597 * |-------------------------------------------------------------|
599 * |<------ length ------->|
601 * = |<----->| + |-------------------------------------| + |---|
602 * start_left st_pgs * i end_left
605 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
606 unsigned long off, unsigned long length)
608 __u64 start = off > st_off ? off - st_off : 0;
609 __u64 end = off + length > st_off ? off + length - st_off : 0;
610 unsigned long start_left = 0;
611 unsigned long end_left = 0;
612 unsigned long pg_count;
614 if (st_len == 0 || length == 0 || end == 0)
617 start_left = do_div(start, st_len);
618 if (start_left < st_pgs)
619 start_left = st_pgs - start_left;
623 end_left = do_div(end, st_len);
624 if (end_left > st_pgs)
627 CDEBUG(D_READA, "start "LPU64", end "LPU64" start_left %lu end_left %lu \n",
628 start, end, start_left, end_left);
631 pg_count = end_left - (st_pgs - start_left);
633 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
635 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
636 "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
641 static int ria_page_count(struct ra_io_arg *ria)
643 __u64 length = ria->ria_end >= ria->ria_start ?
644 ria->ria_end - ria->ria_start + 1 : 0;
646 return stride_pg_count(ria->ria_stoff, ria->ria_length,
647 ria->ria_pages, ria->ria_start,
651 /*Check whether the index is in the defined ra-window */
652 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
654 /* If ria_length == ria_pages, it means non-stride I/O mode,
655 * idx should always inside read-ahead window in this case
656 * For stride I/O mode, just check whether the idx is inside
658 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
659 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
660 ria->ria_length < ria->ria_pages);
663 static int ll_read_ahead_pages(const struct lu_env *env,
664 struct cl_io *io, struct cl_page_list *queue,
665 struct ra_io_arg *ria,
666 unsigned long *reserved_pages,
667 struct address_space *mapping,
668 unsigned long *ra_end)
670 int rc, count = 0, stride_ria;
671 unsigned long page_idx;
673 LASSERT(ria != NULL);
676 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
677 for (page_idx = ria->ria_start; page_idx <= ria->ria_end &&
678 *reserved_pages > 0; page_idx++) {
679 if (ras_inside_ra_window(page_idx, ria)) {
680 /* If the page is inside the read-ahead window*/
681 rc = ll_read_ahead_page(env, io, queue,
686 } else if (rc == -ENOLCK)
688 } else if (stride_ria) {
689 /* If it is not in the read-ahead window, and it is
690 * read-ahead mode, then check whether it should skip
693 /* FIXME: This assertion only is valid when it is for
694 * forward read-ahead, it will be fixed when backward
695 * read-ahead is implemented */
696 LASSERTF(page_idx > ria->ria_stoff, "Invalid page_idx %lu"
697 "rs %lu re %lu ro %lu rl %lu rp %lu\n", page_idx,
698 ria->ria_start, ria->ria_end, ria->ria_stoff,
699 ria->ria_length, ria->ria_pages);
700 offset = page_idx - ria->ria_stoff;
701 offset = offset % (ria->ria_length);
702 if (offset > ria->ria_pages) {
703 page_idx += ria->ria_length - offset;
704 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
705 ria->ria_length - offset);
714 int ll_readahead(const struct lu_env *env, struct cl_io *io,
715 struct ll_readahead_state *ras, struct address_space *mapping,
716 struct cl_page_list *queue, int flags)
718 struct vvp_io *vio = vvp_env_io(env);
719 struct vvp_thread_info *vti = vvp_env_info(env);
720 struct cl_attr *attr = ccc_env_thread_attr(env);
721 unsigned long start = 0, end = 0, reserved;
722 unsigned long ra_end, len;
724 struct ll_ra_read *bead;
725 struct ra_io_arg *ria = &vti->vti_ria;
726 struct ll_inode_info *lli;
727 struct cl_object *clob;
732 inode = mapping->host;
733 lli = ll_i2info(inode);
734 clob = lli->lli_clob;
736 memset(ria, 0, sizeof *ria);
738 cl_object_attr_lock(clob);
739 ret = cl_object_attr_get(env, clob, attr);
740 cl_object_attr_unlock(clob);
746 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
750 cfs_spin_lock(&ras->ras_lock);
751 if (vio->cui_ra_window_set)
752 bead = &vio->cui_bead;
756 /* Enlarge the RA window to encompass the full read */
757 if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
758 bead->lrr_start + bead->lrr_count) {
759 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
760 ras->ras_window_start;
762 /* Reserve a part of the read-ahead window that we'll be issuing */
763 if (ras->ras_window_len) {
764 start = ras->ras_next_readahead;
765 end = ras->ras_window_start + ras->ras_window_len - 1;
768 unsigned long rpc_boundary;
770 * Align RA window to an optimal boundary.
772 * XXX This would be better to align to cl_max_pages_per_rpc
773 * instead of PTLRPC_MAX_BRW_PAGES, because the RPC size may
774 * be aligned to the RAID stripe size in the future and that
775 * is more important than the RPC size.
777 /* Note: we only trim the RPC, instead of extending the RPC
778 * to the boundary, so to avoid reading too much pages during
780 rpc_boundary = ((end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1)));
781 if (rpc_boundary > 0)
784 if (rpc_boundary > start)
787 /* Truncate RA window to end of file */
788 end = min(end, (unsigned long)((kms - 1) >> CFS_PAGE_SHIFT));
790 ras->ras_next_readahead = max(end, end + 1);
793 ria->ria_start = start;
795 /* If stride I/O mode is detected, get stride window*/
796 if (stride_io_mode(ras)) {
797 ria->ria_stoff = ras->ras_stride_offset;
798 ria->ria_length = ras->ras_stride_length;
799 ria->ria_pages = ras->ras_stride_pages;
801 cfs_spin_unlock(&ras->ras_lock);
804 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
807 len = ria_page_count(ria);
811 reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len);
813 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
815 CDEBUG(D_READA, "reserved page %lu ra_cur %d ra_max %lu\n", reserved,
816 cfs_atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
817 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
819 ret = ll_read_ahead_pages(env, io, queue,
820 ria, &reserved, mapping, &ra_end);
822 LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
824 ll_ra_count_put(ll_i2sbi(inode), reserved);
826 if (ra_end == end + 1 && ra_end == (kms >> CFS_PAGE_SHIFT))
827 ll_ra_stats_inc(mapping, RA_STAT_EOF);
829 /* if we didn't get to the end of the region we reserved from
830 * the ras we need to go back and update the ras so that the
831 * next read-ahead tries from where we left off. we only do so
832 * if the region we failed to issue read-ahead on is still ahead
833 * of the app and behind the next index to start read-ahead from */
834 CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
835 ra_end, end, ria->ria_end);
837 if (ra_end != end + 1) {
838 cfs_spin_lock(&ras->ras_lock);
839 if (ra_end < ras->ras_next_readahead &&
840 index_in_window(ra_end, ras->ras_window_start, 0,
841 ras->ras_window_len)) {
842 ras->ras_next_readahead = ra_end;
845 cfs_spin_unlock(&ras->ras_lock);
851 static void ras_set_start(struct ll_readahead_state *ras, unsigned long index)
853 ras->ras_window_start = index & (~(RAS_INCREASE_STEP - 1));
856 /* called with the ras_lock held or from places where it doesn't matter */
857 static void ras_reset(struct ll_readahead_state *ras, unsigned long index)
859 ras->ras_last_readpage = index;
860 ras->ras_consecutive_requests = 0;
861 ras->ras_consecutive_pages = 0;
862 ras->ras_window_len = 0;
863 ras_set_start(ras, index);
864 ras->ras_next_readahead = max(ras->ras_window_start, index);
869 /* called with the ras_lock held or from places where it doesn't matter */
870 static void ras_stride_reset(struct ll_readahead_state *ras)
872 ras->ras_consecutive_stride_requests = 0;
873 ras->ras_stride_length = 0;
874 ras->ras_stride_pages = 0;
878 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
880 cfs_spin_lock_init(&ras->ras_lock);
882 ras->ras_requests = 0;
883 CFS_INIT_LIST_HEAD(&ras->ras_read_beads);
887 * Check whether the read request is in the stride window.
888 * If it is in the stride window, return 1, otherwise return 0.
890 static int index_in_stride_window(unsigned long index,
891 struct ll_readahead_state *ras,
894 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
896 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
897 ras->ras_stride_pages == ras->ras_stride_length)
900 /* If it is contiguous read */
902 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
904 /*Otherwise check the stride by itself */
905 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
906 ras->ras_consecutive_pages == ras->ras_stride_pages;
909 static void ras_update_stride_detector(struct ll_readahead_state *ras,
912 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
914 if (!stride_io_mode(ras) && (stride_gap != 0 ||
915 ras->ras_consecutive_stride_requests == 0)) {
916 ras->ras_stride_pages = ras->ras_consecutive_pages;
917 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
919 LASSERT(ras->ras_request_index == 0);
920 LASSERT(ras->ras_consecutive_stride_requests == 0);
922 if (index <= ras->ras_last_readpage) {
923 /*Reset stride window for forward read*/
924 ras_stride_reset(ras);
928 ras->ras_stride_pages = ras->ras_consecutive_pages;
929 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
936 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
938 return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
939 ras->ras_stride_pages, ras->ras_stride_offset,
943 /* Stride Read-ahead window will be increased inc_len according to
944 * stride I/O pattern */
945 static void ras_stride_increase_window(struct ll_readahead_state *ras,
946 struct ll_ra_info *ra,
947 unsigned long inc_len)
949 unsigned long left, step, window_len;
950 unsigned long stride_len;
952 LASSERT(ras->ras_stride_length > 0);
953 LASSERTF(ras->ras_window_start + ras->ras_window_len
954 >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
955 " stride_offset %lu\n", ras->ras_window_start,
956 ras->ras_window_len, ras->ras_stride_offset);
958 stride_len = ras->ras_window_start + ras->ras_window_len -
959 ras->ras_stride_offset;
961 left = stride_len % ras->ras_stride_length;
962 window_len = ras->ras_window_len - left;
964 if (left < ras->ras_stride_pages)
967 left = ras->ras_stride_pages + inc_len;
969 LASSERT(ras->ras_stride_pages != 0);
971 step = left / ras->ras_stride_pages;
972 left %= ras->ras_stride_pages;
974 window_len += step * ras->ras_stride_length + left;
976 if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
977 ras->ras_window_len = window_len;
982 static void ras_increase_window(struct ll_readahead_state *ras,
983 struct ll_ra_info *ra, struct inode *inode)
985 /* The stretch of ra-window should be aligned with max rpc_size
986 * but current clio architecture does not support retrieve such
987 * information from lower layer. FIXME later
989 if (stride_io_mode(ras))
990 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP);
992 ras->ras_window_len = min(ras->ras_window_len +
994 ra->ra_max_pages_per_file);
997 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
998 struct ll_readahead_state *ras, unsigned long index,
1001 struct ll_ra_info *ra = &sbi->ll_ra_info;
1002 int zero = 0, stride_detect = 0, ra_miss = 0;
1005 cfs_spin_lock(&ras->ras_lock);
1007 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
1009 /* reset the read-ahead window in two cases. First when the app seeks
1010 * or reads to some other part of the file. Secondly if we get a
1011 * read-ahead miss that we think we've previously issued. This can
1012 * be a symptom of there being so many read-ahead pages that the VM is
1013 * reclaiming it before we get to it. */
1014 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
1016 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
1017 } else if (!hit && ras->ras_window_len &&
1018 index < ras->ras_next_readahead &&
1019 index_in_window(index, ras->ras_window_start, 0,
1020 ras->ras_window_len)) {
1022 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
1025 /* On the second access to a file smaller than the tunable
1026 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
1027 * file up to ra_max_pages_per_file. This is simply a best effort
1028 * and only occurs once per open file. Normal RA behavior is reverted
1029 * to for subsequent IO. The mmap case does not increment
1030 * ras_requests and thus can never trigger this behavior. */
1031 if (ras->ras_requests == 2 && !ras->ras_request_index) {
1034 kms_pages = (i_size_read(inode) + CFS_PAGE_SIZE - 1) >>
1037 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
1038 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
1041 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
1042 ras->ras_window_start = 0;
1043 ras->ras_last_readpage = 0;
1044 ras->ras_next_readahead = 0;
1045 ras->ras_window_len = min(ra->ra_max_pages_per_file,
1046 ra->ra_max_read_ahead_whole_pages);
1047 GOTO(out_unlock, 0);
1051 /* check whether it is in stride I/O mode*/
1052 if (!index_in_stride_window(index, ras, inode)) {
1053 if (ras->ras_consecutive_stride_requests == 0 &&
1054 ras->ras_request_index == 0) {
1055 ras_update_stride_detector(ras, index);
1056 ras->ras_consecutive_stride_requests ++;
1058 ras_stride_reset(ras);
1060 ras_reset(ras, index);
1061 ras->ras_consecutive_pages++;
1062 GOTO(out_unlock, 0);
1064 ras->ras_consecutive_pages = 0;
1065 ras->ras_consecutive_requests = 0;
1066 if (++ras->ras_consecutive_stride_requests > 1)
1072 if (index_in_stride_window(index, ras, inode) &&
1073 stride_io_mode(ras)) {
1074 /*If stride-RA hit cache miss, the stride dector
1075 *will not be reset to avoid the overhead of
1076 *redetecting read-ahead mode */
1077 if (index != ras->ras_last_readpage + 1)
1078 ras->ras_consecutive_pages = 0;
1079 ras_reset(ras, index);
1082 /* Reset both stride window and normal RA
1084 ras_reset(ras, index);
1085 ras->ras_consecutive_pages++;
1086 ras_stride_reset(ras);
1087 GOTO(out_unlock, 0);
1089 } else if (stride_io_mode(ras)) {
1090 /* If this is contiguous read but in stride I/O mode
1091 * currently, check whether stride step still is valid,
1092 * if invalid, it will reset the stride ra window*/
1093 if (!index_in_stride_window(index, ras, inode)) {
1094 /* Shrink stride read-ahead window to be zero */
1095 ras_stride_reset(ras);
1096 ras->ras_window_len = 0;
1097 ras->ras_next_readahead = index;
1101 ras->ras_consecutive_pages++;
1102 ras->ras_last_readpage = index;
1103 ras_set_start(ras, index);
1105 if (stride_io_mode(ras))
1106 /* Since stride readahead is sentivite to the offset
1107 * of read-ahead, so we use original offset here,
1108 * instead of ras_window_start, which is 1M aligned*/
1109 ras->ras_next_readahead = max(index,
1110 ras->ras_next_readahead);
1112 ras->ras_next_readahead = max(ras->ras_window_start,
1113 ras->ras_next_readahead);
1116 /* Trigger RA in the mmap case where ras_consecutive_requests
1117 * is not incremented and thus can't be used to trigger RA */
1118 if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
1119 ras->ras_window_len = RAS_INCREASE_STEP;
1120 GOTO(out_unlock, 0);
1123 /* Initially reset the stride window offset to next_readahead*/
1124 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
1126 * Once stride IO mode is detected, next_readahead should be
1127 * reset to make sure next_readahead > stride offset
1129 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
1130 ras->ras_stride_offset = index;
1131 ras->ras_window_len = RAS_INCREASE_STEP;
1134 /* The initial ras_window_len is set to the request size. To avoid
1135 * uselessly reading and discarding pages for random IO the window is
1136 * only increased once per consecutive request received. */
1137 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
1138 !ras->ras_request_index)
1139 ras_increase_window(ras, ra, inode);
1143 ras->ras_request_index++;
1144 cfs_spin_unlock(&ras->ras_lock);
1148 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1150 struct inode *inode = vmpage->mapping->host;
1153 struct cl_page *page;
1154 struct cl_object *clob;
1155 struct cl_2queue *queue;
1156 struct cl_env_nest nest;
1160 LASSERT(PageLocked(vmpage));
1161 LASSERT(!PageWriteback(vmpage));
1163 if (ll_i2dtexp(inode) == NULL)
1166 env = cl_env_nested_get(&nest);
1168 RETURN(PTR_ERR(env));
1170 queue = &vvp_env_info(env)->vti_queue;
1171 clob = ll_i2info(inode)->lli_clob;
1172 LASSERT(clob != NULL);
1174 io = ccc_env_thread_io(env);
1176 result = cl_io_init(env, io, CIT_MISC, clob);
1178 page = cl_page_find(env, clob, vmpage->index,
1179 vmpage, CPT_CACHEABLE);
1180 if (!IS_ERR(page)) {
1181 lu_ref_add(&page->cp_reference, "writepage",
1183 cl_page_assume(env, io, page);
1185 * Mark page dirty, because this is what
1186 * ->vio_submit()->cpo_prep_write() assumes.
1188 * XXX better solution is to detect this from within
1189 * cl_io_submit_rw() somehow.
1191 set_page_dirty(vmpage);
1192 cl_2queue_init_page(queue, page);
1193 result = cl_io_submit_rw(env, io, CRT_WRITE,
1197 * Re-dirty page on error so it retries write,
1198 * but not in case when IO has actually
1199 * occurred and completed with an error.
1201 if (!PageError(vmpage)) {
1202 redirty_page_for_writepage(wbc, vmpage);
1206 cl_page_list_disown(env, io, &queue->c2_qin);
1207 LASSERT(!cl_page_is_owned(page, io));
1208 lu_ref_del(&page->cp_reference,
1209 "writepage", cfs_current());
1210 cl_page_put(env, page);
1211 cl_2queue_fini(env, queue);
1214 cl_io_fini(env, io);
1215 cl_env_nested_put(&nest, env);
1219 int ll_readpage(struct file *file, struct page *vmpage)
1221 struct ll_cl_context *lcc;
1225 lcc = ll_cl_init(file, vmpage, 0);
1227 struct lu_env *env = lcc->lcc_env;
1228 struct cl_io *io = lcc->lcc_io;
1229 struct cl_page *page = lcc->lcc_page;
1231 LASSERT(page->cp_type == CPT_CACHEABLE);
1232 if (likely(!PageUptodate(vmpage))) {
1233 cl_page_assume(env, io, page);
1234 result = cl_io_read_page(env, io, page);
1236 /* Page from a non-object file. */
1237 LASSERT(!ll_i2info(vmpage->mapping->host)->lli_smd);
1238 unlock_page(vmpage);
1243 unlock_page(vmpage);
1244 result = PTR_ERR(lcc);