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) {
144 struct inode *inode = vmpage->mapping->host;
147 if (TRYLOCK_INODE_MUTEX(inode)) {
148 UNLOCK_INODE_MUTEX(inode);
150 /* this is too bad. Someone is trying to write the
151 * page w/o holding inode mutex. This means we can
152 * add dirty pages into cache during truncate */
153 CERROR("Proc %s is dirting page w/o inode lock, this"
154 "will break truncate.\n", cfs_current()->comm);
155 libcfs_debug_dumpstack(NULL);
157 return ERR_PTR(-EIO);
161 * Loop-back driver calls ->prepare_write() and ->sendfile()
162 * methods directly, bypassing file system ->write() operation,
163 * so cl_io has to be created here.
165 io = ccc_env_thread_io(env);
166 ll_io_init(io, file, 1);
168 /* No lock at all for this kind of IO - we can't do it because
169 * we have held page lock, it would cause deadlock.
170 * XXX: This causes poor performance to loop device - One page
172 * In order to get better performance, users should use
173 * lloop driver instead.
175 io->ci_lockreq = CILR_NEVER;
177 pos = (vmpage->index << CFS_PAGE_SHIFT);
179 /* Create a temp IO to serve write. */
180 result = cl_io_rw_init(env, io, CIT_WRITE, pos, CFS_PAGE_SIZE);
182 cio->cui_fd = LUSTRE_FPRIVATE(file);
185 result = cl_io_iter_init(env, io);
187 result = cl_io_lock(env, io);
189 result = cl_io_start(env, io);
192 result = io->ci_result;
193 lcc->lcc_created = 1;
200 struct cl_page *page;
203 LASSERT(io->ci_state == CIS_IO_GOING);
204 LASSERT(cio->cui_fd == LUSTRE_FPRIVATE(file));
205 page = cl_page_find(env, clob, vmpage->index, vmpage,
208 lcc->lcc_page = page;
209 lu_ref_add(&page->cp_reference, "cl_io", io);
212 result = PTR_ERR(page);
216 lcc = ERR_PTR(result);
219 CDEBUG(D_VFSTRACE, "%lu@"DFID" -> %d %p %p\n",
220 vmpage->index, PFID(lu_object_fid(&clob->co_lu)), result,
225 static struct ll_cl_context *ll_cl_get(void)
227 struct ll_cl_context *lcc;
231 env = cl_env_get(&refcheck);
232 LASSERT(!IS_ERR(env));
233 lcc = &vvp_env_info(env)->vti_io_ctx;
234 LASSERT(env == lcc->lcc_env);
235 LASSERT(current == lcc->lcc_cookie);
236 cl_env_put(env, &refcheck);
238 /* env has got in ll_cl_init, so it is still usable. */
243 * ->prepare_write() address space operation called by generic_file_write()
244 * for every page during write.
246 int ll_prepare_write(struct file *file, struct page *vmpage, unsigned from,
249 struct ll_cl_context *lcc;
253 lcc = ll_cl_init(file, vmpage, 1);
255 struct lu_env *env = lcc->lcc_env;
256 struct cl_io *io = lcc->lcc_io;
257 struct cl_page *page = lcc->lcc_page;
259 cl_page_assume(env, io, page);
260 if (cl_io_is_append(io)) {
261 struct cl_object *obj = io->ci_obj;
262 struct inode *inode = ccc_object_inode(obj);
264 * In VFS file->page write loop, for appending, the
265 * write offset might be reset according to the new
266 * file size before holding i_mutex. So crw_pos should
267 * be reset here. BUG:17711.
269 io->u.ci_wr.wr.crw_pos = i_size_read(inode);
271 result = cl_io_prepare_write(env, io, page, from, to);
274 * Add a reference, so that page is not evicted from
275 * the cache until ->commit_write() is called.
278 lu_ref_add(&page->cp_reference, "prepare_write",
281 cl_page_unassume(env, io, page);
284 /* returning 0 in prepare assumes commit must be called
287 result = PTR_ERR(lcc);
292 int ll_commit_write(struct file *file, struct page *vmpage, unsigned from,
295 struct ll_cl_context *lcc;
298 struct cl_page *page;
304 page = lcc->lcc_page;
307 LASSERT(cl_page_is_owned(page, io));
309 if (from != to) /* handle short write case. */
310 result = cl_io_commit_write(env, io, page, from, to);
311 if (cl_page_is_owned(page, io))
312 cl_page_unassume(env, io, page);
315 * Release reference acquired by ll_prepare_write().
317 lu_ref_del(&page->cp_reference, "prepare_write", cfs_current());
318 cl_page_put(env, page);
323 struct obd_capa *cl_capa_lookup(struct inode *inode, enum cl_req_type crt)
327 opc = crt == CRT_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
328 return ll_osscapa_get(inode, opc);
331 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
334 * Get readahead pages from the filesystem readahead pool of the client for a
337 * /param sbi superblock for filesystem readahead state ll_ra_info
338 * /param ria per-thread readahead state
339 * /param pages number of pages requested for readahead for the thread.
341 * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
342 * It should work well if the ra_max_pages is much greater than the single
343 * file's read-ahead window, and not too many threads contending for
344 * these readahead pages.
346 * TODO: There may be a 'global sync problem' if many threads are trying
347 * to get an ra budget that is larger than the remaining readahead pages
348 * and reach here at exactly the same time. They will compute /a ret to
349 * consume the remaining pages, but will fail at atomic_add_return() and
350 * get a zero ra window, although there is still ra space remaining. - Jay */
352 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
353 struct ra_io_arg *ria,
356 struct ll_ra_info *ra = &sbi->ll_ra_info;
360 /* If read-ahead pages left are less than 1M, do not do read-ahead,
361 * otherwise it will form small read RPC(< 1M), which hurt server
362 * performance a lot. */
363 ret = min(ra->ra_max_pages - cfs_atomic_read(&ra->ra_cur_pages), pages);
364 if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages))
367 /* If the non-strided (ria_pages == 0) readahead window
368 * (ria_start + ret) has grown across an RPC boundary, then trim
369 * readahead size by the amount beyond the RPC so it ends on an
370 * RPC boundary. If the readahead window is already ending on
371 * an RPC boundary (beyond_rpc == 0), or smaller than a full
372 * RPC (beyond_rpc < ret) the readahead size is unchanged.
373 * The (beyond_rpc != 0) check is skipped since the conditional
374 * branch is more expensive than subtracting zero from the result.
376 * Strided read is left unaligned to avoid small fragments beyond
377 * the RPC boundary from needing an extra read RPC. */
378 if (ria->ria_pages == 0) {
379 long beyond_rpc = (ria->ria_start + ret) % PTLRPC_MAX_BRW_PAGES;
380 if (/* beyond_rpc != 0 && */ beyond_rpc < ret)
384 if (cfs_atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
385 cfs_atomic_sub(ret, &ra->ra_cur_pages);
393 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
395 struct ll_ra_info *ra = &sbi->ll_ra_info;
396 cfs_atomic_sub(len, &ra->ra_cur_pages);
399 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
401 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
402 lprocfs_counter_incr(sbi->ll_ra_stats, which);
405 void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
407 struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
408 ll_ra_stats_inc_sbi(sbi, which);
411 #define RAS_CDEBUG(ras) \
413 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu" \
414 "csr %lu sf %lu sp %lu sl %lu \n", \
415 ras->ras_last_readpage, ras->ras_consecutive_requests, \
416 ras->ras_consecutive_pages, ras->ras_window_start, \
417 ras->ras_window_len, ras->ras_next_readahead, \
418 ras->ras_requests, ras->ras_request_index, \
419 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
420 ras->ras_stride_pages, ras->ras_stride_length)
422 static int index_in_window(unsigned long index, unsigned long point,
423 unsigned long before, unsigned long after)
425 unsigned long start = point - before, end = point + after;
432 return start <= index && index <= end;
435 static struct ll_readahead_state *ll_ras_get(struct file *f)
437 struct ll_file_data *fd;
439 fd = LUSTRE_FPRIVATE(f);
443 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar)
445 struct ll_readahead_state *ras;
449 cfs_spin_lock(&ras->ras_lock);
451 ras->ras_request_index = 0;
452 ras->ras_consecutive_requests++;
453 rar->lrr_reader = current;
455 cfs_list_add(&rar->lrr_linkage, &ras->ras_read_beads);
456 cfs_spin_unlock(&ras->ras_lock);
459 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
461 struct ll_readahead_state *ras;
465 cfs_spin_lock(&ras->ras_lock);
466 cfs_list_del_init(&rar->lrr_linkage);
467 cfs_spin_unlock(&ras->ras_lock);
470 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
472 struct ll_ra_read *scan;
474 cfs_list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
475 if (scan->lrr_reader == current)
481 struct ll_ra_read *ll_ra_read_get(struct file *f)
483 struct ll_readahead_state *ras;
484 struct ll_ra_read *bead;
488 cfs_spin_lock(&ras->ras_lock);
489 bead = ll_ra_read_get_locked(ras);
490 cfs_spin_unlock(&ras->ras_lock);
494 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
495 struct cl_page_list *queue, struct cl_page *page,
504 cl_page_assume(env, io, page);
505 lu_ref_add(&page->cp_reference, "ra", cfs_current());
506 cp = cl2ccc_page(cl_page_at(page, &vvp_device_type));
507 if (!cp->cpg_defer_uptodate && !Page_Uptodate(vmpage)) {
508 rc = cl_page_is_under_lock(env, io, page);
510 cp->cpg_defer_uptodate = 1;
512 cl_page_list_add(queue, page);
515 cl_page_delete(env, page);
519 /* skip completed pages */
520 cl_page_unassume(env, io, page);
521 lu_ref_del(&page->cp_reference, "ra", cfs_current());
522 cl_page_put(env, page);
527 * Initiates read-ahead of a page with given index.
529 * \retval +ve: page was added to \a queue.
531 * \retval -ENOLCK: there is no extent lock for this part of a file, stop
534 * \retval -ve, 0: page wasn't added to \a queue for other reason.
536 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
537 struct cl_page_list *queue,
538 pgoff_t index, struct address_space *mapping)
541 struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
542 struct cl_page *page;
543 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
544 unsigned int gfp_mask;
546 const char *msg = NULL;
550 gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
552 gfp_mask |= __GFP_NOWARN;
554 vmpage = grab_cache_page_nowait(mapping, index);
555 if (vmpage != NULL) {
556 /* Check if vmpage was truncated or reclaimed */
557 if (vmpage->mapping == mapping) {
558 page = cl_page_find(env, clob, vmpage->index,
559 vmpage, CPT_CACHEABLE);
561 rc = cl_read_ahead_page(env, io, queue,
564 which = RA_STAT_FAILED_MATCH;
565 msg = "lock match failed";
568 which = RA_STAT_FAILED_GRAB_PAGE;
569 msg = "cl_page_find failed";
572 which = RA_STAT_WRONG_GRAB_PAGE;
573 msg = "g_c_p_n returned invalid page";
577 page_cache_release(vmpage);
579 which = RA_STAT_FAILED_GRAB_PAGE;
580 msg = "g_c_p_n failed";
583 ll_ra_stats_inc(mapping, which);
584 CDEBUG(D_READA, "%s\n", msg);
589 #define RIA_DEBUG(ria) \
590 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
591 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
594 #define RAS_INCREASE_STEP PTLRPC_MAX_BRW_PAGES
596 static inline int stride_io_mode(struct ll_readahead_state *ras)
598 return ras->ras_consecutive_stride_requests > 1;
600 /* The function calculates how much pages will be read in
601 * [off, off + length], in such stride IO area,
602 * stride_offset = st_off, stride_lengh = st_len,
603 * stride_pages = st_pgs
605 * |------------------|*****|------------------|*****|------------|*****|....
608 * |----- st_len -----|
610 * How many pages it should read in such pattern
611 * |-------------------------------------------------------------|
613 * |<------ length ------->|
615 * = |<----->| + |-------------------------------------| + |---|
616 * start_left st_pgs * i end_left
619 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
620 unsigned long off, unsigned long length)
622 __u64 start = off > st_off ? off - st_off : 0;
623 __u64 end = off + length > st_off ? off + length - st_off : 0;
624 unsigned long start_left = 0;
625 unsigned long end_left = 0;
626 unsigned long pg_count;
628 if (st_len == 0 || length == 0 || end == 0)
631 start_left = do_div(start, st_len);
632 if (start_left < st_pgs)
633 start_left = st_pgs - start_left;
637 end_left = do_div(end, st_len);
638 if (end_left > st_pgs)
641 CDEBUG(D_READA, "start "LPU64", end "LPU64" start_left %lu end_left %lu \n",
642 start, end, start_left, end_left);
645 pg_count = end_left - (st_pgs - start_left);
647 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
649 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
650 "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
655 static int ria_page_count(struct ra_io_arg *ria)
657 __u64 length = ria->ria_end >= ria->ria_start ?
658 ria->ria_end - ria->ria_start + 1 : 0;
660 return stride_pg_count(ria->ria_stoff, ria->ria_length,
661 ria->ria_pages, ria->ria_start,
665 /*Check whether the index is in the defined ra-window */
666 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
668 /* If ria_length == ria_pages, it means non-stride I/O mode,
669 * idx should always inside read-ahead window in this case
670 * For stride I/O mode, just check whether the idx is inside
672 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
673 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
674 ria->ria_length < ria->ria_pages);
677 static int ll_read_ahead_pages(const struct lu_env *env,
678 struct cl_io *io, struct cl_page_list *queue,
679 struct ra_io_arg *ria,
680 unsigned long *reserved_pages,
681 struct address_space *mapping,
682 unsigned long *ra_end)
684 int rc, count = 0, stride_ria;
685 unsigned long page_idx;
687 LASSERT(ria != NULL);
690 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
691 for (page_idx = ria->ria_start; page_idx <= ria->ria_end &&
692 *reserved_pages > 0; page_idx++) {
693 if (ras_inside_ra_window(page_idx, ria)) {
694 /* If the page is inside the read-ahead window*/
695 rc = ll_read_ahead_page(env, io, queue,
700 } else if (rc == -ENOLCK)
702 } else if (stride_ria) {
703 /* If it is not in the read-ahead window, and it is
704 * read-ahead mode, then check whether it should skip
707 /* FIXME: This assertion only is valid when it is for
708 * forward read-ahead, it will be fixed when backward
709 * read-ahead is implemented */
710 LASSERTF(page_idx > ria->ria_stoff, "Invalid page_idx %lu"
711 "rs %lu re %lu ro %lu rl %lu rp %lu\n", page_idx,
712 ria->ria_start, ria->ria_end, ria->ria_stoff,
713 ria->ria_length, ria->ria_pages);
714 offset = page_idx - ria->ria_stoff;
715 offset = offset % (ria->ria_length);
716 if (offset > ria->ria_pages) {
717 page_idx += ria->ria_length - offset;
718 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
719 ria->ria_length - offset);
728 int ll_readahead(const struct lu_env *env, struct cl_io *io,
729 struct ll_readahead_state *ras, struct address_space *mapping,
730 struct cl_page_list *queue, int flags)
732 struct vvp_io *vio = vvp_env_io(env);
733 struct vvp_thread_info *vti = vvp_env_info(env);
734 struct cl_attr *attr = ccc_env_thread_attr(env);
735 unsigned long start = 0, end = 0, reserved;
736 unsigned long ra_end, len;
738 struct ll_ra_read *bead;
739 struct ra_io_arg *ria = &vti->vti_ria;
740 struct ll_inode_info *lli;
741 struct cl_object *clob;
746 inode = mapping->host;
747 lli = ll_i2info(inode);
748 clob = lli->lli_clob;
750 memset(ria, 0, sizeof *ria);
752 cl_object_attr_lock(clob);
753 ret = cl_object_attr_get(env, clob, attr);
754 cl_object_attr_unlock(clob);
760 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
764 cfs_spin_lock(&ras->ras_lock);
765 if (vio->cui_ra_window_set)
766 bead = &vio->cui_bead;
770 /* Enlarge the RA window to encompass the full read */
771 if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
772 bead->lrr_start + bead->lrr_count) {
773 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
774 ras->ras_window_start;
776 /* Reserve a part of the read-ahead window that we'll be issuing */
777 if (ras->ras_window_len) {
778 start = ras->ras_next_readahead;
779 end = ras->ras_window_start + ras->ras_window_len - 1;
782 unsigned long rpc_boundary;
784 * Align RA window to an optimal boundary.
786 * XXX This would be better to align to cl_max_pages_per_rpc
787 * instead of PTLRPC_MAX_BRW_PAGES, because the RPC size may
788 * be aligned to the RAID stripe size in the future and that
789 * is more important than the RPC size.
791 /* Note: we only trim the RPC, instead of extending the RPC
792 * to the boundary, so to avoid reading too much pages during
794 rpc_boundary = ((end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1)));
795 if (rpc_boundary > 0)
798 if (rpc_boundary > start)
801 /* Truncate RA window to end of file */
802 end = min(end, (unsigned long)((kms - 1) >> CFS_PAGE_SHIFT));
804 ras->ras_next_readahead = max(end, end + 1);
807 ria->ria_start = start;
809 /* If stride I/O mode is detected, get stride window*/
810 if (stride_io_mode(ras)) {
811 ria->ria_stoff = ras->ras_stride_offset;
812 ria->ria_length = ras->ras_stride_length;
813 ria->ria_pages = ras->ras_stride_pages;
815 cfs_spin_unlock(&ras->ras_lock);
818 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
821 len = ria_page_count(ria);
825 reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len);
827 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
829 CDEBUG(D_READA, "reserved page %lu ra_cur %d ra_max %lu\n", reserved,
830 cfs_atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
831 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
833 ret = ll_read_ahead_pages(env, io, queue,
834 ria, &reserved, mapping, &ra_end);
836 LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
838 ll_ra_count_put(ll_i2sbi(inode), reserved);
840 if (ra_end == end + 1 && ra_end == (kms >> CFS_PAGE_SHIFT))
841 ll_ra_stats_inc(mapping, RA_STAT_EOF);
843 /* if we didn't get to the end of the region we reserved from
844 * the ras we need to go back and update the ras so that the
845 * next read-ahead tries from where we left off. we only do so
846 * if the region we failed to issue read-ahead on is still ahead
847 * of the app and behind the next index to start read-ahead from */
848 CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
849 ra_end, end, ria->ria_end);
851 if (ra_end != end + 1) {
852 cfs_spin_lock(&ras->ras_lock);
853 if (ra_end < ras->ras_next_readahead &&
854 index_in_window(ra_end, ras->ras_window_start, 0,
855 ras->ras_window_len)) {
856 ras->ras_next_readahead = ra_end;
859 cfs_spin_unlock(&ras->ras_lock);
865 static void ras_set_start(struct ll_readahead_state *ras, unsigned long index)
867 ras->ras_window_start = index & (~(RAS_INCREASE_STEP - 1));
870 /* called with the ras_lock held or from places where it doesn't matter */
871 static void ras_reset(struct ll_readahead_state *ras, unsigned long index)
873 ras->ras_last_readpage = index;
874 ras->ras_consecutive_requests = 0;
875 ras->ras_consecutive_pages = 0;
876 ras->ras_window_len = 0;
877 ras_set_start(ras, index);
878 ras->ras_next_readahead = max(ras->ras_window_start, index);
883 /* called with the ras_lock held or from places where it doesn't matter */
884 static void ras_stride_reset(struct ll_readahead_state *ras)
886 ras->ras_consecutive_stride_requests = 0;
887 ras->ras_stride_length = 0;
888 ras->ras_stride_pages = 0;
892 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
894 cfs_spin_lock_init(&ras->ras_lock);
896 ras->ras_requests = 0;
897 CFS_INIT_LIST_HEAD(&ras->ras_read_beads);
901 * Check whether the read request is in the stride window.
902 * If it is in the stride window, return 1, otherwise return 0.
904 static int index_in_stride_window(unsigned long index,
905 struct ll_readahead_state *ras,
908 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
910 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
911 ras->ras_stride_pages == ras->ras_stride_length)
914 /* If it is contiguous read */
916 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
918 /*Otherwise check the stride by itself */
919 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
920 ras->ras_consecutive_pages == ras->ras_stride_pages;
923 static void ras_update_stride_detector(struct ll_readahead_state *ras,
926 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
928 if (!stride_io_mode(ras) && (stride_gap != 0 ||
929 ras->ras_consecutive_stride_requests == 0)) {
930 ras->ras_stride_pages = ras->ras_consecutive_pages;
931 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
933 LASSERT(ras->ras_request_index == 0);
934 LASSERT(ras->ras_consecutive_stride_requests == 0);
936 if (index <= ras->ras_last_readpage) {
937 /*Reset stride window for forward read*/
938 ras_stride_reset(ras);
942 ras->ras_stride_pages = ras->ras_consecutive_pages;
943 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
950 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
952 return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
953 ras->ras_stride_pages, ras->ras_stride_offset,
957 /* Stride Read-ahead window will be increased inc_len according to
958 * stride I/O pattern */
959 static void ras_stride_increase_window(struct ll_readahead_state *ras,
960 struct ll_ra_info *ra,
961 unsigned long inc_len)
963 unsigned long left, step, window_len;
964 unsigned long stride_len;
966 LASSERT(ras->ras_stride_length > 0);
967 LASSERTF(ras->ras_window_start + ras->ras_window_len
968 >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
969 " stride_offset %lu\n", ras->ras_window_start,
970 ras->ras_window_len, ras->ras_stride_offset);
972 stride_len = ras->ras_window_start + ras->ras_window_len -
973 ras->ras_stride_offset;
975 left = stride_len % ras->ras_stride_length;
976 window_len = ras->ras_window_len - left;
978 if (left < ras->ras_stride_pages)
981 left = ras->ras_stride_pages + inc_len;
983 LASSERT(ras->ras_stride_pages != 0);
985 step = left / ras->ras_stride_pages;
986 left %= ras->ras_stride_pages;
988 window_len += step * ras->ras_stride_length + left;
990 if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
991 ras->ras_window_len = window_len;
996 static void ras_increase_window(struct ll_readahead_state *ras,
997 struct ll_ra_info *ra, struct inode *inode)
999 /* The stretch of ra-window should be aligned with max rpc_size
1000 * but current clio architecture does not support retrieve such
1001 * information from lower layer. FIXME later
1003 if (stride_io_mode(ras))
1004 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP);
1006 ras->ras_window_len = min(ras->ras_window_len +
1008 ra->ra_max_pages_per_file);
1011 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
1012 struct ll_readahead_state *ras, unsigned long index,
1015 struct ll_ra_info *ra = &sbi->ll_ra_info;
1016 int zero = 0, stride_detect = 0, ra_miss = 0;
1019 cfs_spin_lock(&ras->ras_lock);
1021 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
1023 /* reset the read-ahead window in two cases. First when the app seeks
1024 * or reads to some other part of the file. Secondly if we get a
1025 * read-ahead miss that we think we've previously issued. This can
1026 * be a symptom of there being so many read-ahead pages that the VM is
1027 * reclaiming it before we get to it. */
1028 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
1030 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
1031 } else if (!hit && ras->ras_window_len &&
1032 index < ras->ras_next_readahead &&
1033 index_in_window(index, ras->ras_window_start, 0,
1034 ras->ras_window_len)) {
1036 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
1039 /* On the second access to a file smaller than the tunable
1040 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
1041 * file up to ra_max_pages_per_file. This is simply a best effort
1042 * and only occurs once per open file. Normal RA behavior is reverted
1043 * to for subsequent IO. The mmap case does not increment
1044 * ras_requests and thus can never trigger this behavior. */
1045 if (ras->ras_requests == 2 && !ras->ras_request_index) {
1048 kms_pages = (i_size_read(inode) + CFS_PAGE_SIZE - 1) >>
1051 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
1052 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
1055 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
1056 ras->ras_window_start = 0;
1057 ras->ras_last_readpage = 0;
1058 ras->ras_next_readahead = 0;
1059 ras->ras_window_len = min(ra->ra_max_pages_per_file,
1060 ra->ra_max_read_ahead_whole_pages);
1061 GOTO(out_unlock, 0);
1065 /* check whether it is in stride I/O mode*/
1066 if (!index_in_stride_window(index, ras, inode)) {
1067 if (ras->ras_consecutive_stride_requests == 0 &&
1068 ras->ras_request_index == 0) {
1069 ras_update_stride_detector(ras, index);
1070 ras->ras_consecutive_stride_requests ++;
1072 ras_stride_reset(ras);
1074 ras_reset(ras, index);
1075 ras->ras_consecutive_pages++;
1076 GOTO(out_unlock, 0);
1078 ras->ras_consecutive_pages = 0;
1079 ras->ras_consecutive_requests = 0;
1080 if (++ras->ras_consecutive_stride_requests > 1)
1086 if (index_in_stride_window(index, ras, inode) &&
1087 stride_io_mode(ras)) {
1088 /*If stride-RA hit cache miss, the stride dector
1089 *will not be reset to avoid the overhead of
1090 *redetecting read-ahead mode */
1091 if (index != ras->ras_last_readpage + 1)
1092 ras->ras_consecutive_pages = 0;
1093 ras_reset(ras, index);
1096 /* Reset both stride window and normal RA
1098 ras_reset(ras, index);
1099 ras->ras_consecutive_pages++;
1100 ras_stride_reset(ras);
1101 GOTO(out_unlock, 0);
1103 } else if (stride_io_mode(ras)) {
1104 /* If this is contiguous read but in stride I/O mode
1105 * currently, check whether stride step still is valid,
1106 * if invalid, it will reset the stride ra window*/
1107 if (!index_in_stride_window(index, ras, inode)) {
1108 /* Shrink stride read-ahead window to be zero */
1109 ras_stride_reset(ras);
1110 ras->ras_window_len = 0;
1111 ras->ras_next_readahead = index;
1115 ras->ras_consecutive_pages++;
1116 ras->ras_last_readpage = index;
1117 ras_set_start(ras, index);
1119 if (stride_io_mode(ras))
1120 /* Since stride readahead is sentivite to the offset
1121 * of read-ahead, so we use original offset here,
1122 * instead of ras_window_start, which is 1M aligned*/
1123 ras->ras_next_readahead = max(index,
1124 ras->ras_next_readahead);
1126 ras->ras_next_readahead = max(ras->ras_window_start,
1127 ras->ras_next_readahead);
1130 /* Trigger RA in the mmap case where ras_consecutive_requests
1131 * is not incremented and thus can't be used to trigger RA */
1132 if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
1133 ras->ras_window_len = RAS_INCREASE_STEP;
1134 GOTO(out_unlock, 0);
1137 /* Initially reset the stride window offset to next_readahead*/
1138 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
1140 * Once stride IO mode is detected, next_readahead should be
1141 * reset to make sure next_readahead > stride offset
1143 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
1144 ras->ras_stride_offset = index;
1145 ras->ras_window_len = RAS_INCREASE_STEP;
1148 /* The initial ras_window_len is set to the request size. To avoid
1149 * uselessly reading and discarding pages for random IO the window is
1150 * only increased once per consecutive request received. */
1151 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
1152 !ras->ras_request_index)
1153 ras_increase_window(ras, ra, inode);
1157 ras->ras_request_index++;
1158 cfs_spin_unlock(&ras->ras_lock);
1162 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1164 struct inode *inode = vmpage->mapping->host;
1167 struct cl_page *page;
1168 struct cl_object *clob;
1169 struct cl_env_nest nest;
1174 LASSERT(PageLocked(vmpage));
1175 LASSERT(!PageWriteback(vmpage));
1177 if (ll_i2dtexp(inode) == NULL)
1180 env = cl_env_nested_get(&nest);
1182 RETURN(PTR_ERR(env));
1184 clob = ll_i2info(inode)->lli_clob;
1185 LASSERT(clob != NULL);
1187 io = ccc_env_thread_io(env);
1189 result = cl_io_init(env, io, CIT_MISC, clob);
1191 page = cl_page_find(env, clob, vmpage->index,
1192 vmpage, CPT_CACHEABLE);
1193 if (!IS_ERR(page)) {
1194 lu_ref_add(&page->cp_reference, "writepage",
1196 cl_page_assume(env, io, page);
1197 result = cl_page_flush(env, io, page);
1200 * Re-dirty page on error so it retries write,
1201 * but not in case when IO has actually
1202 * occurred and completed with an error.
1204 if (!PageError(vmpage)) {
1205 redirty_page_for_writepage(wbc, vmpage);
1210 cl_page_disown(env, io, page);
1211 lu_ref_del(&page->cp_reference,
1212 "writepage", cfs_current());
1213 cl_page_put(env, page);
1216 cl_io_fini(env, io);
1218 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1219 loff_t offset = cl_offset(clob, vmpage->index);
1221 /* Flush page failed because the extent is being written out.
1222 * Wait for the write of extent to be finished to avoid
1223 * breaking kernel which assumes ->writepage should mark
1224 * PageWriteback or clean the page. */
1225 result = cl_sync_file_range(inode, offset,
1226 offset + CFS_PAGE_SIZE - 1,
1229 /* actually we may have written more than one page.
1230 * decreasing this page because the caller will count
1232 wbc->nr_to_write -= result - 1;
1237 cl_env_nested_put(&nest, env);
1241 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1243 struct inode *inode = mapping->host;
1246 enum cl_fsync_mode mode;
1247 int range_whole = 0;
1251 if (wbc->range_cyclic) {
1252 start = mapping->writeback_index << CFS_PAGE_SHIFT;
1253 end = OBD_OBJECT_EOF;
1255 start = wbc->range_start;
1256 end = wbc->range_end;
1257 if (end == LLONG_MAX) {
1258 end = OBD_OBJECT_EOF;
1259 range_whole = start == 0;
1263 mode = CL_FSYNC_NONE;
1264 if (wbc->sync_mode == WB_SYNC_ALL)
1265 mode = CL_FSYNC_LOCAL;
1267 result = cl_sync_file_range(inode, start, end, mode);
1269 wbc->nr_to_write -= result;
1273 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1274 if (end == OBD_OBJECT_EOF)
1275 end = i_size_read(inode);
1276 mapping->writeback_index = (end >> CFS_PAGE_SHIFT) + 1;
1281 int ll_readpage(struct file *file, struct page *vmpage)
1283 struct ll_cl_context *lcc;
1287 lcc = ll_cl_init(file, vmpage, 0);
1289 struct lu_env *env = lcc->lcc_env;
1290 struct cl_io *io = lcc->lcc_io;
1291 struct cl_page *page = lcc->lcc_page;
1293 LASSERT(page->cp_type == CPT_CACHEABLE);
1294 if (likely(!PageUptodate(vmpage))) {
1295 cl_page_assume(env, io, page);
1296 result = cl_io_read_page(env, io, page);
1298 /* Page from a non-object file. */
1299 LASSERT(!ll_i2info(vmpage->mapping->host)->lli_smd);
1300 unlock_page(vmpage);
1305 unlock_page(vmpage);
1306 result = PTR_ERR(lcc);