1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
40 * Lustre Lite I/O page cache routines shared by different kernel revs
43 #include <linux/kernel.h>
45 #include <linux/string.h>
46 #include <linux/stat.h>
47 #include <linux/errno.h>
48 #include <linux/smp_lock.h>
49 #include <linux/unistd.h>
50 #include <linux/version.h>
51 #include <asm/system.h>
52 #include <asm/uaccess.h>
55 #include <linux/stat.h>
56 #include <asm/uaccess.h>
58 #include <linux/pagemap.h>
59 #include <linux/smp_lock.h>
60 /* current_is_kswapd() */
61 #include <linux/swap.h>
63 #define DEBUG_SUBSYSTEM S_LLITE
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)
80 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) to %llu\n", inode->i_ino,
81 inode->i_generation, inode, i_size_read(inode));
83 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_TRUNC, 1);
90 * Finalizes cl-data before exiting typical address_space operation. Dual to
93 static void ll_cl_fini(struct ll_cl_context *lcc)
95 struct lu_env *env = lcc->lcc_env;
96 struct cl_io *io = lcc->lcc_io;
97 struct cl_page *page = lcc->lcc_page;
99 LASSERT(lcc->lcc_cookie == current);
100 LASSERT(env != NULL);
103 lu_ref_del(&page->cp_reference, "cl_io", io);
104 cl_page_put(env, page);
107 if (io && lcc->lcc_created) {
109 cl_io_unlock(env, io);
110 cl_io_iter_fini(env, io);
113 cl_env_put(env, &lcc->lcc_refcheck);
117 * Initializes common cl-data at the typical address_space operation entry
120 static struct ll_cl_context *ll_cl_init(struct file *file,
121 struct page *vmpage, int create)
123 struct ll_cl_context *lcc;
126 struct cl_object *clob;
132 clob = ll_i2info(vmpage->mapping->host)->lli_clob;
133 LASSERT(clob != NULL);
135 env = cl_env_get(&refcheck);
137 return ERR_PTR(PTR_ERR(env));
139 lcc = &vvp_env_info(env)->vti_io_ctx;
140 memset(lcc, 0, sizeof(*lcc));
142 lcc->lcc_refcheck = refcheck;
143 lcc->lcc_cookie = current;
145 cio = ccc_env_io(env);
146 io = cio->cui_cl.cis_io;
147 if (io == NULL && create) {
151 * Loop-back driver calls ->prepare_write() and ->sendfile()
152 * methods directly, bypassing file system ->write() operation,
153 * so cl_io has to be created here.
155 io = ccc_env_thread_io(env);
156 ll_io_init(io, file, 1);
158 /* No lock at all for this kind of IO - we can't do it because
159 * we have held page lock, it would cause deadlock.
160 * XXX: This causes poor performance to loop device - One page
162 * In order to get better performance, users should use
163 * lloop driver instead.
165 io->ci_lockreq = CILR_NEVER;
167 pos = (vmpage->index << CFS_PAGE_SHIFT);
169 /* Create a temp IO to serve write. */
170 result = cl_io_rw_init(env, io, CIT_WRITE, pos, CFS_PAGE_SIZE);
172 cio->cui_fd = LUSTRE_FPRIVATE(file);
175 result = cl_io_iter_init(env, io);
177 result = cl_io_lock(env, io);
179 result = cl_io_start(env, io);
182 result = io->ci_result;
183 lcc->lcc_created = 1;
190 struct cl_page *page;
193 LASSERT(io->ci_state == CIS_IO_GOING);
194 LASSERT(cio->cui_fd == LUSTRE_FPRIVATE(file));
195 page = cl_page_find(env, clob, vmpage->index, vmpage,
198 lcc->lcc_page = page;
199 lu_ref_add(&page->cp_reference, "cl_io", io);
202 result = PTR_ERR(page);
206 lcc = ERR_PTR(result);
209 CDEBUG(D_VFSTRACE, "%lu@"DFID" -> %d %p %p\n",
210 vmpage->index, PFID(lu_object_fid(&clob->co_lu)), result,
215 static struct ll_cl_context *ll_cl_get(void)
217 struct ll_cl_context *lcc;
221 env = cl_env_get(&refcheck);
222 LASSERT(!IS_ERR(env));
223 lcc = &vvp_env_info(env)->vti_io_ctx;
224 LASSERT(env == lcc->lcc_env);
225 LASSERT(current == lcc->lcc_cookie);
226 cl_env_put(env, &refcheck);
228 /* env has got in ll_cl_init, so it is still usable. */
233 * ->prepare_write() address space operation called by generic_file_write()
234 * for every page during write.
236 int ll_prepare_write(struct file *file, struct page *vmpage, unsigned from,
239 struct ll_cl_context *lcc;
243 lcc = ll_cl_init(file, vmpage, 1);
245 struct lu_env *env = lcc->lcc_env;
246 struct cl_io *io = lcc->lcc_io;
247 struct cl_page *page = lcc->lcc_page;
249 cl_page_assume(env, io, page);
250 if (cl_io_is_append(io)) {
251 struct cl_object *obj = io->ci_obj;
252 struct inode *inode = ccc_object_inode(obj);
254 * In VFS file->page write loop, for appending, the
255 * write offset might be reset according to the new
256 * file size before holding i_mutex. So crw_pos should
257 * be reset here. BUG:17711.
259 io->u.ci_wr.wr.crw_pos = i_size_read(inode);
261 result = cl_io_prepare_write(env, io, page, from, to);
264 * Add a reference, so that page is not evicted from
265 * the cache until ->commit_write() is called.
268 lu_ref_add(&page->cp_reference, "prepare_write",
271 cl_page_unassume(env, io, page);
274 /* returning 0 in prepare assumes commit must be called
277 result = PTR_ERR(lcc);
282 int ll_commit_write(struct file *file, struct page *vmpage, unsigned from,
285 struct ll_cl_context *lcc;
288 struct cl_page *page;
294 page = lcc->lcc_page;
297 LASSERT(cl_page_is_owned(page, io));
299 if (from != to) /* handle short write case. */
300 result = cl_io_commit_write(env, io, page, from, to);
301 if (cl_page_is_owned(page, io))
302 cl_page_unassume(env, io, page);
305 * Release reference acquired by ll_prepare_write().
307 lu_ref_del(&page->cp_reference, "prepare_write", cfs_current());
308 cl_page_put(env, page);
313 struct obd_capa *cl_capa_lookup(struct inode *inode, enum cl_req_type crt)
317 opc = crt == CRT_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
318 return ll_osscapa_get(inode, opc);
321 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
324 * Get readahead pages from the filesystem readahead pool of the client for a
327 * /param sbi superblock for filesystem readahead state ll_ra_info
328 * /param ria per-thread readahead state
329 * /param pages number of pages requested for readahead for the thread.
331 * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
332 * It should work well if the ra_max_pages is much greater than the single
333 * file's read-ahead window, and not too many threads contending for
334 * these readahead pages.
336 * TODO: There may be a 'global sync problem' if many threads are trying
337 * to get an ra budget that is larger than the remaining readahead pages
338 * and reach here at exactly the same time. They will compute /a ret to
339 * consume the remaining pages, but will fail at atomic_add_return() and
340 * get a zero ra window, although there is still ra space remaining. - Jay */
342 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
343 struct ra_io_arg *ria,
346 struct ll_ra_info *ra = &sbi->ll_ra_info;
350 /* If read-ahead pages left are less than 1M, do not do read-ahead,
351 * otherwise it will form small read RPC(< 1M), which hurt server
352 * performance a lot. */
353 ret = min(ra->ra_max_pages - cfs_atomic_read(&ra->ra_cur_pages), pages);
354 if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages))
357 /* If the non-strided (ria_pages == 0) readahead window
358 * (ria_start + ret) has grown across an RPC boundary, then trim
359 * readahead size by the amount beyond the RPC so it ends on an
360 * RPC boundary. If the readahead window is already ending on
361 * an RPC boundary (beyond_rpc == 0), or smaller than a full
362 * RPC (beyond_rpc < ret) the readahead size is unchanged.
363 * The (beyond_rpc != 0) check is skipped since the conditional
364 * branch is more expensive than subtracting zero from the result.
366 * Strided read is left unaligned to avoid small fragments beyond
367 * the RPC boundary from needing an extra read RPC. */
368 if (ria->ria_pages == 0) {
369 long beyond_rpc = (ria->ria_start + ret) % PTLRPC_MAX_BRW_PAGES;
370 if (/* beyond_rpc != 0 && */ beyond_rpc < ret)
374 if (cfs_atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
375 cfs_atomic_sub(ret, &ra->ra_cur_pages);
383 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
385 struct ll_ra_info *ra = &sbi->ll_ra_info;
386 cfs_atomic_sub(len, &ra->ra_cur_pages);
389 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
391 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
392 lprocfs_counter_incr(sbi->ll_ra_stats, which);
395 void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
397 struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
398 ll_ra_stats_inc_sbi(sbi, which);
401 #define RAS_CDEBUG(ras) \
403 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu" \
404 "csr %lu sf %lu sp %lu sl %lu \n", \
405 ras->ras_last_readpage, ras->ras_consecutive_requests, \
406 ras->ras_consecutive_pages, ras->ras_window_start, \
407 ras->ras_window_len, ras->ras_next_readahead, \
408 ras->ras_requests, ras->ras_request_index, \
409 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
410 ras->ras_stride_pages, ras->ras_stride_length)
412 static int index_in_window(unsigned long index, unsigned long point,
413 unsigned long before, unsigned long after)
415 unsigned long start = point - before, end = point + after;
422 return start <= index && index <= end;
425 static struct ll_readahead_state *ll_ras_get(struct file *f)
427 struct ll_file_data *fd;
429 fd = LUSTRE_FPRIVATE(f);
433 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar)
435 struct ll_readahead_state *ras;
439 cfs_spin_lock(&ras->ras_lock);
441 ras->ras_request_index = 0;
442 ras->ras_consecutive_requests++;
443 rar->lrr_reader = current;
445 cfs_list_add(&rar->lrr_linkage, &ras->ras_read_beads);
446 cfs_spin_unlock(&ras->ras_lock);
449 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
451 struct ll_readahead_state *ras;
455 cfs_spin_lock(&ras->ras_lock);
456 cfs_list_del_init(&rar->lrr_linkage);
457 cfs_spin_unlock(&ras->ras_lock);
460 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
462 struct ll_ra_read *scan;
464 cfs_list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
465 if (scan->lrr_reader == current)
471 struct ll_ra_read *ll_ra_read_get(struct file *f)
473 struct ll_readahead_state *ras;
474 struct ll_ra_read *bead;
478 cfs_spin_lock(&ras->ras_lock);
479 bead = ll_ra_read_get_locked(ras);
480 cfs_spin_unlock(&ras->ras_lock);
484 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
485 struct cl_page_list *queue, struct cl_page *page,
494 cl_page_assume(env, io, page);
495 lu_ref_add(&page->cp_reference, "ra", cfs_current());
496 cp = cl2ccc_page(cl_page_at(page, &vvp_device_type));
497 if (!cp->cpg_defer_uptodate && !Page_Uptodate(vmpage)) {
498 rc = cl_page_is_under_lock(env, io, page);
500 cp->cpg_defer_uptodate = 1;
502 cl_page_list_add(queue, page);
505 cl_page_delete(env, page);
509 /* skip completed pages */
510 cl_page_unassume(env, io, page);
511 lu_ref_del(&page->cp_reference, "ra", cfs_current());
512 cl_page_put(env, page);
517 * Initiates read-ahead of a page with given index.
519 * \retval +ve: page was added to \a queue.
521 * \retval -ENOLCK: there is no extent lock for this part of a file, stop
524 * \retval -ve, 0: page wasn't added to \a queue for other reason.
526 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
527 struct cl_page_list *queue,
528 pgoff_t index, struct address_space *mapping)
531 struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
532 struct cl_page *page;
533 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
534 unsigned int gfp_mask;
536 const char *msg = NULL;
540 gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
542 gfp_mask |= __GFP_NOWARN;
544 vmpage = grab_cache_page_nowait_gfp(mapping, index, gfp_mask);
545 if (vmpage != NULL) {
546 /* Check if vmpage was truncated or reclaimed */
547 if (vmpage->mapping == mapping) {
548 page = cl_page_find(env, clob, vmpage->index,
549 vmpage, CPT_CACHEABLE);
551 rc = cl_read_ahead_page(env, io, queue,
554 which = RA_STAT_FAILED_MATCH;
555 msg = "lock match failed";
558 which = RA_STAT_FAILED_GRAB_PAGE;
559 msg = "cl_page_find failed";
562 which = RA_STAT_WRONG_GRAB_PAGE;
563 msg = "g_c_p_n returned invalid page";
567 page_cache_release(vmpage);
569 which = RA_STAT_FAILED_GRAB_PAGE;
570 msg = "g_c_p_n failed";
573 ll_ra_stats_inc(mapping, which);
574 CDEBUG(D_READA, "%s\n", msg);
579 #define RIA_DEBUG(ria) \
580 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
581 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
584 #define RAS_INCREASE_STEP PTLRPC_MAX_BRW_PAGES
586 static inline int stride_io_mode(struct ll_readahead_state *ras)
588 return ras->ras_consecutive_stride_requests > 1;
590 /* The function calculates how much pages will be read in
591 * [off, off + length], in such stride IO area,
592 * stride_offset = st_off, stride_lengh = st_len,
593 * stride_pages = st_pgs
595 * |------------------|*****|------------------|*****|------------|*****|....
598 * |----- st_len -----|
600 * How many pages it should read in such pattern
601 * |-------------------------------------------------------------|
603 * |<------ length ------->|
605 * = |<----->| + |-------------------------------------| + |---|
606 * start_left st_pgs * i end_left
609 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
610 unsigned long off, unsigned long length)
612 __u64 start = off > st_off ? off - st_off : 0;
613 __u64 end = off + length > st_off ? off + length - st_off : 0;
614 unsigned long start_left = 0;
615 unsigned long end_left = 0;
616 unsigned long pg_count;
618 if (st_len == 0 || length == 0 || end == 0)
621 start_left = do_div(start, st_len);
622 if (start_left < st_pgs)
623 start_left = st_pgs - start_left;
627 end_left = do_div(end, st_len);
628 if (end_left > st_pgs)
631 CDEBUG(D_READA, "start "LPU64", end "LPU64" start_left %lu end_left %lu \n",
632 start, end, start_left, end_left);
635 pg_count = end_left - (st_pgs - start_left);
637 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
639 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
640 "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
645 static int ria_page_count(struct ra_io_arg *ria)
647 __u64 length = ria->ria_end >= ria->ria_start ?
648 ria->ria_end - ria->ria_start + 1 : 0;
650 return stride_pg_count(ria->ria_stoff, ria->ria_length,
651 ria->ria_pages, ria->ria_start,
655 /*Check whether the index is in the defined ra-window */
656 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
658 /* If ria_length == ria_pages, it means non-stride I/O mode,
659 * idx should always inside read-ahead window in this case
660 * For stride I/O mode, just check whether the idx is inside
662 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
663 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
664 ria->ria_length < ria->ria_pages);
667 static int ll_read_ahead_pages(const struct lu_env *env,
668 struct cl_io *io, struct cl_page_list *queue,
669 struct ra_io_arg *ria,
670 unsigned long *reserved_pages,
671 struct address_space *mapping,
672 unsigned long *ra_end)
674 int rc, count = 0, stride_ria;
675 unsigned long page_idx;
677 LASSERT(ria != NULL);
680 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
681 for (page_idx = ria->ria_start; page_idx <= ria->ria_end &&
682 *reserved_pages > 0; page_idx++) {
683 if (ras_inside_ra_window(page_idx, ria)) {
684 /* If the page is inside the read-ahead window*/
685 rc = ll_read_ahead_page(env, io, queue,
690 } else if (rc == -ENOLCK)
692 } else if (stride_ria) {
693 /* If it is not in the read-ahead window, and it is
694 * read-ahead mode, then check whether it should skip
697 /* FIXME: This assertion only is valid when it is for
698 * forward read-ahead, it will be fixed when backward
699 * read-ahead is implemented */
700 LASSERTF(page_idx > ria->ria_stoff, "Invalid page_idx %lu"
701 "rs %lu re %lu ro %lu rl %lu rp %lu\n", page_idx,
702 ria->ria_start, ria->ria_end, ria->ria_stoff,
703 ria->ria_length, ria->ria_pages);
704 offset = page_idx - ria->ria_stoff;
705 offset = offset % (ria->ria_length);
706 if (offset > ria->ria_pages) {
707 page_idx += ria->ria_length - offset;
708 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
709 ria->ria_length - offset);
718 int ll_readahead(const struct lu_env *env, struct cl_io *io,
719 struct ll_readahead_state *ras, struct address_space *mapping,
720 struct cl_page_list *queue, int flags)
722 struct vvp_io *vio = vvp_env_io(env);
723 struct vvp_thread_info *vti = vvp_env_info(env);
724 struct cl_attr *attr = ccc_env_thread_attr(env);
725 unsigned long start = 0, end = 0, reserved;
726 unsigned long ra_end, len;
728 struct ll_ra_read *bead;
729 struct ra_io_arg *ria = &vti->vti_ria;
730 struct ll_inode_info *lli;
731 struct cl_object *clob;
736 inode = mapping->host;
737 lli = ll_i2info(inode);
738 clob = lli->lli_clob;
740 memset(ria, 0, sizeof *ria);
742 cl_object_attr_lock(clob);
743 ret = cl_object_attr_get(env, clob, attr);
744 cl_object_attr_unlock(clob);
750 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
754 cfs_spin_lock(&ras->ras_lock);
755 if (vio->cui_ra_window_set)
756 bead = &vio->cui_bead;
760 /* Enlarge the RA window to encompass the full read */
761 if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
762 bead->lrr_start + bead->lrr_count) {
763 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
764 ras->ras_window_start;
766 /* Reserve a part of the read-ahead window that we'll be issuing */
767 if (ras->ras_window_len) {
768 start = ras->ras_next_readahead;
769 end = ras->ras_window_start + ras->ras_window_len - 1;
772 unsigned long rpc_boundary;
774 * Align RA window to an optimal boundary.
776 * XXX This would be better to align to cl_max_pages_per_rpc
777 * instead of PTLRPC_MAX_BRW_PAGES, because the RPC size may
778 * be aligned to the RAID stripe size in the future and that
779 * is more important than the RPC size.
781 /* Note: we only trim the RPC, instead of extending the RPC
782 * to the boundary, so to avoid reading too much pages during
784 rpc_boundary = ((end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1)));
785 if (rpc_boundary > 0)
788 if (rpc_boundary > start)
791 /* Truncate RA window to end of file */
792 end = min(end, (unsigned long)((kms - 1) >> CFS_PAGE_SHIFT));
794 ras->ras_next_readahead = max(end, end + 1);
797 ria->ria_start = start;
799 /* If stride I/O mode is detected, get stride window*/
800 if (stride_io_mode(ras)) {
801 ria->ria_stoff = ras->ras_stride_offset;
802 ria->ria_length = ras->ras_stride_length;
803 ria->ria_pages = ras->ras_stride_pages;
805 cfs_spin_unlock(&ras->ras_lock);
808 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
811 len = ria_page_count(ria);
815 reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len);
817 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
819 CDEBUG(D_READA, "reserved page %lu ra_cur %d ra_max %lu\n", reserved,
820 cfs_atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
821 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
823 ret = ll_read_ahead_pages(env, io, queue,
824 ria, &reserved, mapping, &ra_end);
826 LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
828 ll_ra_count_put(ll_i2sbi(inode), reserved);
830 if (ra_end == end + 1 && ra_end == (kms >> CFS_PAGE_SHIFT))
831 ll_ra_stats_inc(mapping, RA_STAT_EOF);
833 /* if we didn't get to the end of the region we reserved from
834 * the ras we need to go back and update the ras so that the
835 * next read-ahead tries from where we left off. we only do so
836 * if the region we failed to issue read-ahead on is still ahead
837 * of the app and behind the next index to start read-ahead from */
838 CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
839 ra_end, end, ria->ria_end);
841 if (ra_end != end + 1) {
842 cfs_spin_lock(&ras->ras_lock);
843 if (ra_end < ras->ras_next_readahead &&
844 index_in_window(ra_end, ras->ras_window_start, 0,
845 ras->ras_window_len)) {
846 ras->ras_next_readahead = ra_end;
849 cfs_spin_unlock(&ras->ras_lock);
855 static void ras_set_start(struct ll_readahead_state *ras, unsigned long index)
857 ras->ras_window_start = index & (~(RAS_INCREASE_STEP - 1));
860 /* called with the ras_lock held or from places where it doesn't matter */
861 static void ras_reset(struct ll_readahead_state *ras, unsigned long index)
863 ras->ras_last_readpage = index;
864 ras->ras_consecutive_requests = 0;
865 ras->ras_consecutive_pages = 0;
866 ras->ras_window_len = 0;
867 ras_set_start(ras, index);
868 ras->ras_next_readahead = max(ras->ras_window_start, index);
873 /* called with the ras_lock held or from places where it doesn't matter */
874 static void ras_stride_reset(struct ll_readahead_state *ras)
876 ras->ras_consecutive_stride_requests = 0;
877 ras->ras_stride_length = 0;
878 ras->ras_stride_pages = 0;
882 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
884 cfs_spin_lock_init(&ras->ras_lock);
886 ras->ras_requests = 0;
887 CFS_INIT_LIST_HEAD(&ras->ras_read_beads);
891 * Check whether the read request is in the stride window.
892 * If it is in the stride window, return 1, otherwise return 0.
894 static int index_in_stride_window(unsigned long index,
895 struct ll_readahead_state *ras,
898 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
900 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
901 ras->ras_stride_pages == ras->ras_stride_length)
904 /* If it is contiguous read */
906 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
908 /*Otherwise check the stride by itself */
909 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
910 ras->ras_consecutive_pages == ras->ras_stride_pages;
913 static void ras_update_stride_detector(struct ll_readahead_state *ras,
916 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
918 if (!stride_io_mode(ras) && (stride_gap != 0 ||
919 ras->ras_consecutive_stride_requests == 0)) {
920 ras->ras_stride_pages = ras->ras_consecutive_pages;
921 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
923 LASSERT(ras->ras_request_index == 0);
924 LASSERT(ras->ras_consecutive_stride_requests == 0);
926 if (index <= ras->ras_last_readpage) {
927 /*Reset stride window for forward read*/
928 ras_stride_reset(ras);
932 ras->ras_stride_pages = ras->ras_consecutive_pages;
933 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
940 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
942 return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
943 ras->ras_stride_pages, ras->ras_stride_offset,
947 /* Stride Read-ahead window will be increased inc_len according to
948 * stride I/O pattern */
949 static void ras_stride_increase_window(struct ll_readahead_state *ras,
950 struct ll_ra_info *ra,
951 unsigned long inc_len)
953 unsigned long left, step, window_len;
954 unsigned long stride_len;
956 LASSERT(ras->ras_stride_length > 0);
957 LASSERTF(ras->ras_window_start + ras->ras_window_len
958 >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
959 " stride_offset %lu\n", ras->ras_window_start,
960 ras->ras_window_len, ras->ras_stride_offset);
962 stride_len = ras->ras_window_start + ras->ras_window_len -
963 ras->ras_stride_offset;
965 left = stride_len % ras->ras_stride_length;
966 window_len = ras->ras_window_len - left;
968 if (left < ras->ras_stride_pages)
971 left = ras->ras_stride_pages + inc_len;
973 LASSERT(ras->ras_stride_pages != 0);
975 step = left / ras->ras_stride_pages;
976 left %= ras->ras_stride_pages;
978 window_len += step * ras->ras_stride_length + left;
980 if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
981 ras->ras_window_len = window_len;
986 static void ras_increase_window(struct ll_readahead_state *ras,
987 struct ll_ra_info *ra, struct inode *inode)
989 /* The stretch of ra-window should be aligned with max rpc_size
990 * but current clio architecture does not support retrieve such
991 * information from lower layer. FIXME later
993 if (stride_io_mode(ras))
994 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP);
996 ras->ras_window_len = min(ras->ras_window_len +
998 ra->ra_max_pages_per_file);
1001 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
1002 struct ll_readahead_state *ras, unsigned long index,
1005 struct ll_ra_info *ra = &sbi->ll_ra_info;
1006 int zero = 0, stride_detect = 0, ra_miss = 0;
1009 cfs_spin_lock(&ras->ras_lock);
1011 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
1013 /* reset the read-ahead window in two cases. First when the app seeks
1014 * or reads to some other part of the file. Secondly if we get a
1015 * read-ahead miss that we think we've previously issued. This can
1016 * be a symptom of there being so many read-ahead pages that the VM is
1017 * reclaiming it before we get to it. */
1018 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
1020 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
1021 } else if (!hit && ras->ras_window_len &&
1022 index < ras->ras_next_readahead &&
1023 index_in_window(index, ras->ras_window_start, 0,
1024 ras->ras_window_len)) {
1026 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
1029 /* On the second access to a file smaller than the tunable
1030 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
1031 * file up to ra_max_pages_per_file. This is simply a best effort
1032 * and only occurs once per open file. Normal RA behavior is reverted
1033 * to for subsequent IO. The mmap case does not increment
1034 * ras_requests and thus can never trigger this behavior. */
1035 if (ras->ras_requests == 2 && !ras->ras_request_index) {
1038 kms_pages = (i_size_read(inode) + CFS_PAGE_SIZE - 1) >>
1041 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
1042 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
1045 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
1046 ras->ras_window_start = 0;
1047 ras->ras_last_readpage = 0;
1048 ras->ras_next_readahead = 0;
1049 ras->ras_window_len = min(ra->ra_max_pages_per_file,
1050 ra->ra_max_read_ahead_whole_pages);
1051 GOTO(out_unlock, 0);
1055 /* check whether it is in stride I/O mode*/
1056 if (!index_in_stride_window(index, ras, inode)) {
1057 if (ras->ras_consecutive_stride_requests == 0 &&
1058 ras->ras_request_index == 0) {
1059 ras_update_stride_detector(ras, index);
1060 ras->ras_consecutive_stride_requests ++;
1062 ras_stride_reset(ras);
1064 ras_reset(ras, index);
1065 ras->ras_consecutive_pages++;
1066 GOTO(out_unlock, 0);
1068 ras->ras_consecutive_pages = 0;
1069 ras->ras_consecutive_requests = 0;
1070 if (++ras->ras_consecutive_stride_requests > 1)
1076 if (index_in_stride_window(index, ras, inode) &&
1077 stride_io_mode(ras)) {
1078 /*If stride-RA hit cache miss, the stride dector
1079 *will not be reset to avoid the overhead of
1080 *redetecting read-ahead mode */
1081 if (index != ras->ras_last_readpage + 1)
1082 ras->ras_consecutive_pages = 0;
1083 ras_reset(ras, index);
1086 /* Reset both stride window and normal RA
1088 ras_reset(ras, index);
1089 ras->ras_consecutive_pages++;
1090 ras_stride_reset(ras);
1091 GOTO(out_unlock, 0);
1093 } else if (stride_io_mode(ras)) {
1094 /* If this is contiguous read but in stride I/O mode
1095 * currently, check whether stride step still is valid,
1096 * if invalid, it will reset the stride ra window*/
1097 if (!index_in_stride_window(index, ras, inode)) {
1098 /* Shrink stride read-ahead window to be zero */
1099 ras_stride_reset(ras);
1100 ras->ras_window_len = 0;
1101 ras->ras_next_readahead = index;
1105 ras->ras_consecutive_pages++;
1106 ras->ras_last_readpage = index;
1107 ras_set_start(ras, index);
1109 if (stride_io_mode(ras))
1110 /* Since stride readahead is sentivite to the offset
1111 * of read-ahead, so we use original offset here,
1112 * instead of ras_window_start, which is 1M aligned*/
1113 ras->ras_next_readahead = max(index,
1114 ras->ras_next_readahead);
1116 ras->ras_next_readahead = max(ras->ras_window_start,
1117 ras->ras_next_readahead);
1120 /* Trigger RA in the mmap case where ras_consecutive_requests
1121 * is not incremented and thus can't be used to trigger RA */
1122 if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
1123 ras->ras_window_len = RAS_INCREASE_STEP;
1124 GOTO(out_unlock, 0);
1127 /* Initially reset the stride window offset to next_readahead*/
1128 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
1130 * Once stride IO mode is detected, next_readahead should be
1131 * reset to make sure next_readahead > stride offset
1133 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
1134 ras->ras_stride_offset = index;
1135 ras->ras_window_len = RAS_INCREASE_STEP;
1138 /* The initial ras_window_len is set to the request size. To avoid
1139 * uselessly reading and discarding pages for random IO the window is
1140 * only increased once per consecutive request received. */
1141 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
1142 !ras->ras_request_index)
1143 ras_increase_window(ras, ra, inode);
1147 ras->ras_request_index++;
1148 cfs_spin_unlock(&ras->ras_lock);
1152 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1154 struct inode *inode = vmpage->mapping->host;
1157 struct cl_page *page;
1158 struct cl_object *clob;
1159 struct cl_2queue *queue;
1160 struct cl_env_nest nest;
1164 LASSERT(PageLocked(vmpage));
1165 LASSERT(!PageWriteback(vmpage));
1167 if (ll_i2dtexp(inode) == NULL)
1170 env = cl_env_nested_get(&nest);
1172 RETURN(PTR_ERR(env));
1174 queue = &vvp_env_info(env)->vti_queue;
1175 clob = ll_i2info(inode)->lli_clob;
1176 LASSERT(clob != NULL);
1178 io = ccc_env_thread_io(env);
1180 result = cl_io_init(env, io, CIT_MISC, clob);
1182 page = cl_page_find(env, clob, vmpage->index,
1183 vmpage, CPT_CACHEABLE);
1184 if (!IS_ERR(page)) {
1185 lu_ref_add(&page->cp_reference, "writepage",
1187 cl_page_assume(env, io, page);
1189 * Mark page dirty, because this is what
1190 * ->vio_submit()->cpo_prep_write() assumes.
1192 * XXX better solution is to detect this from within
1193 * cl_io_submit_rw() somehow.
1195 set_page_dirty(vmpage);
1196 cl_2queue_init_page(queue, page);
1197 result = cl_io_submit_rw(env, io, CRT_WRITE,
1201 * Re-dirty page on error so it retries write,
1202 * but not in case when IO has actually
1203 * occurred and completed with an error.
1205 if (!PageError(vmpage)) {
1206 redirty_page_for_writepage(wbc, vmpage);
1210 cl_page_list_disown(env, io, &queue->c2_qin);
1211 LASSERT(!cl_page_is_owned(page, io));
1212 lu_ref_del(&page->cp_reference,
1213 "writepage", cfs_current());
1214 cl_page_put(env, page);
1215 cl_2queue_fini(env, queue);
1218 cl_io_fini(env, io);
1219 cl_env_nested_put(&nest, env);
1223 int ll_readpage(struct file *file, struct page *vmpage)
1225 struct ll_cl_context *lcc;
1229 lcc = ll_cl_init(file, vmpage, 0);
1231 struct lu_env *env = lcc->lcc_env;
1232 struct cl_io *io = lcc->lcc_io;
1233 struct cl_page *page = lcc->lcc_page;
1235 LASSERT(page->cp_type == CPT_CACHEABLE);
1236 if (likely(!PageUptodate(vmpage))) {
1237 cl_page_assume(env, io, page);
1238 result = cl_io_read_page(env, io, page);
1240 /* Page from a non-object file. */
1241 LASSERT(!ll_i2info(vmpage->mapping->host)->lli_smd);
1242 unlock_page(vmpage);
1247 unlock_page(vmpage);
1248 result = PTR_ERR(lcc);