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/unistd.h>
47 #include <linux/writeback.h>
48 #include <asm/system.h>
49 #include <asm/uaccess.h>
52 #include <linux/stat.h>
53 #include <asm/uaccess.h>
55 #include <linux/pagemap.h>
56 /* current_is_kswapd() */
57 #include <linux/swap.h>
59 #define DEBUG_SUBSYSTEM S_LLITE
61 #include <lustre_lite.h>
62 #include <obd_cksum.h>
63 #include "llite_internal.h"
64 #include <linux/lustre_compat25.h>
67 * Finalizes cl-data before exiting typical address_space operation. Dual to
70 static void ll_cl_fini(struct ll_cl_context *lcc)
72 struct lu_env *env = lcc->lcc_env;
73 struct cl_io *io = lcc->lcc_io;
74 struct cl_page *page = lcc->lcc_page;
76 LASSERT(lcc->lcc_cookie == current);
80 lu_ref_del(&page->cp_reference, "cl_io", io);
81 cl_page_put(env, page);
84 if (io && lcc->lcc_created) {
86 cl_io_unlock(env, io);
87 cl_io_iter_fini(env, io);
90 cl_env_put(env, &lcc->lcc_refcheck);
94 * Initializes common cl-data at the typical address_space operation entry
97 static struct ll_cl_context *ll_cl_init(struct file *file,
98 struct page *vmpage, int create)
100 struct ll_cl_context *lcc;
103 struct cl_object *clob;
109 clob = ll_i2info(vmpage->mapping->host)->lli_clob;
110 LASSERT(clob != NULL);
112 env = cl_env_get(&refcheck);
114 return ERR_PTR(PTR_ERR(env));
116 lcc = &vvp_env_info(env)->vti_io_ctx;
117 memset(lcc, 0, sizeof(*lcc));
119 lcc->lcc_refcheck = refcheck;
120 lcc->lcc_cookie = current;
122 cio = ccc_env_io(env);
123 io = cio->cui_cl.cis_io;
124 if (io == NULL && create) {
125 struct inode *inode = vmpage->mapping->host;
128 if (mutex_trylock(&inode->i_mutex)) {
129 mutex_unlock(&(inode)->i_mutex);
131 /* this is too bad. Someone is trying to write the
132 * page w/o holding inode mutex. This means we can
133 * add dirty pages into cache during truncate */
134 CERROR("Proc %s is dirting page w/o inode lock, this"
135 "will break truncate.\n", cfs_current()->comm);
136 libcfs_debug_dumpstack(NULL);
138 return ERR_PTR(-EIO);
142 * Loop-back driver calls ->prepare_write() and ->sendfile()
143 * methods directly, bypassing file system ->write() operation,
144 * so cl_io has to be created here.
146 io = ccc_env_thread_io(env);
147 ll_io_init(io, file, 1);
149 /* No lock at all for this kind of IO - we can't do it because
150 * we have held page lock, it would cause deadlock.
151 * XXX: This causes poor performance to loop device - One page
153 * In order to get better performance, users should use
154 * lloop driver instead.
156 io->ci_lockreq = CILR_NEVER;
158 pos = (vmpage->index << CFS_PAGE_SHIFT);
160 /* Create a temp IO to serve write. */
161 result = cl_io_rw_init(env, io, CIT_WRITE, pos, CFS_PAGE_SIZE);
163 cio->cui_fd = LUSTRE_FPRIVATE(file);
166 result = cl_io_iter_init(env, io);
168 result = cl_io_lock(env, io);
170 result = cl_io_start(env, io);
173 result = io->ci_result;
174 lcc->lcc_created = 1;
181 struct cl_page *page;
184 LASSERT(io->ci_state == CIS_IO_GOING);
185 LASSERT(cio->cui_fd == LUSTRE_FPRIVATE(file));
186 page = cl_page_find(env, clob, vmpage->index, vmpage,
189 lcc->lcc_page = page;
190 lu_ref_add(&page->cp_reference, "cl_io", io);
193 result = PTR_ERR(page);
197 lcc = ERR_PTR(result);
200 CDEBUG(D_VFSTRACE, "%lu@"DFID" -> %d %p %p\n",
201 vmpage->index, PFID(lu_object_fid(&clob->co_lu)), result,
206 static struct ll_cl_context *ll_cl_get(void)
208 struct ll_cl_context *lcc;
212 env = cl_env_get(&refcheck);
213 LASSERT(!IS_ERR(env));
214 lcc = &vvp_env_info(env)->vti_io_ctx;
215 LASSERT(env == lcc->lcc_env);
216 LASSERT(current == lcc->lcc_cookie);
217 cl_env_put(env, &refcheck);
219 /* env has got in ll_cl_init, so it is still usable. */
224 * ->prepare_write() address space operation called by generic_file_write()
225 * for every page during write.
227 int ll_prepare_write(struct file *file, struct page *vmpage, unsigned from,
230 struct ll_cl_context *lcc;
234 lcc = ll_cl_init(file, vmpage, 1);
236 struct lu_env *env = lcc->lcc_env;
237 struct cl_io *io = lcc->lcc_io;
238 struct cl_page *page = lcc->lcc_page;
240 cl_page_assume(env, io, page);
241 if (cl_io_is_append(io)) {
242 struct cl_object *obj = io->ci_obj;
243 struct inode *inode = ccc_object_inode(obj);
245 * In VFS file->page write loop, for appending, the
246 * write offset might be reset according to the new
247 * file size before holding i_mutex. So crw_pos should
248 * be reset here. BUG:17711.
250 io->u.ci_wr.wr.crw_pos = i_size_read(inode);
252 result = cl_io_prepare_write(env, io, page, from, to);
255 * Add a reference, so that page is not evicted from
256 * the cache until ->commit_write() is called.
259 lu_ref_add(&page->cp_reference, "prepare_write",
262 cl_page_unassume(env, io, page);
265 /* returning 0 in prepare assumes commit must be called
268 result = PTR_ERR(lcc);
273 int ll_commit_write(struct file *file, struct page *vmpage, unsigned from,
276 struct ll_cl_context *lcc;
279 struct cl_page *page;
285 page = lcc->lcc_page;
288 LASSERT(cl_page_is_owned(page, io));
290 if (from != to) /* handle short write case. */
291 result = cl_io_commit_write(env, io, page, from, to);
292 if (cl_page_is_owned(page, io))
293 cl_page_unassume(env, io, page);
296 * Release reference acquired by ll_prepare_write().
298 lu_ref_del(&page->cp_reference, "prepare_write", cfs_current());
299 cl_page_put(env, page);
304 struct obd_capa *cl_capa_lookup(struct inode *inode, enum cl_req_type crt)
308 opc = crt == CRT_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
309 return ll_osscapa_get(inode, opc);
312 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
315 * Get readahead pages from the filesystem readahead pool of the client for a
318 * /param sbi superblock for filesystem readahead state ll_ra_info
319 * /param ria per-thread readahead state
320 * /param pages number of pages requested for readahead for the thread.
322 * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
323 * It should work well if the ra_max_pages is much greater than the single
324 * file's read-ahead window, and not too many threads contending for
325 * these readahead pages.
327 * TODO: There may be a 'global sync problem' if many threads are trying
328 * to get an ra budget that is larger than the remaining readahead pages
329 * and reach here at exactly the same time. They will compute /a ret to
330 * consume the remaining pages, but will fail at atomic_add_return() and
331 * get a zero ra window, although there is still ra space remaining. - Jay */
333 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
334 struct ra_io_arg *ria,
337 struct ll_ra_info *ra = &sbi->ll_ra_info;
341 /* If read-ahead pages left are less than 1M, do not do read-ahead,
342 * otherwise it will form small read RPC(< 1M), which hurt server
343 * performance a lot. */
344 ret = min(ra->ra_max_pages - cfs_atomic_read(&ra->ra_cur_pages), pages);
345 if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages))
348 /* If the non-strided (ria_pages == 0) readahead window
349 * (ria_start + ret) has grown across an RPC boundary, then trim
350 * readahead size by the amount beyond the RPC so it ends on an
351 * RPC boundary. If the readahead window is already ending on
352 * an RPC boundary (beyond_rpc == 0), or smaller than a full
353 * RPC (beyond_rpc < ret) the readahead size is unchanged.
354 * The (beyond_rpc != 0) check is skipped since the conditional
355 * branch is more expensive than subtracting zero from the result.
357 * Strided read is left unaligned to avoid small fragments beyond
358 * the RPC boundary from needing an extra read RPC. */
359 if (ria->ria_pages == 0) {
360 long beyond_rpc = (ria->ria_start + ret) % PTLRPC_MAX_BRW_PAGES;
361 if (/* beyond_rpc != 0 && */ beyond_rpc < ret)
365 if (cfs_atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
366 cfs_atomic_sub(ret, &ra->ra_cur_pages);
374 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
376 struct ll_ra_info *ra = &sbi->ll_ra_info;
377 cfs_atomic_sub(len, &ra->ra_cur_pages);
380 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
382 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
383 lprocfs_counter_incr(sbi->ll_ra_stats, which);
386 void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
388 struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
389 ll_ra_stats_inc_sbi(sbi, which);
392 #define RAS_CDEBUG(ras) \
394 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu" \
395 "csr %lu sf %lu sp %lu sl %lu \n", \
396 ras->ras_last_readpage, ras->ras_consecutive_requests, \
397 ras->ras_consecutive_pages, ras->ras_window_start, \
398 ras->ras_window_len, ras->ras_next_readahead, \
399 ras->ras_requests, ras->ras_request_index, \
400 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
401 ras->ras_stride_pages, ras->ras_stride_length)
403 static int index_in_window(unsigned long index, unsigned long point,
404 unsigned long before, unsigned long after)
406 unsigned long start = point - before, end = point + after;
413 return start <= index && index <= end;
416 static struct ll_readahead_state *ll_ras_get(struct file *f)
418 struct ll_file_data *fd;
420 fd = LUSTRE_FPRIVATE(f);
424 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar)
426 struct ll_readahead_state *ras;
430 cfs_spin_lock(&ras->ras_lock);
432 ras->ras_request_index = 0;
433 ras->ras_consecutive_requests++;
434 rar->lrr_reader = current;
436 cfs_list_add(&rar->lrr_linkage, &ras->ras_read_beads);
437 cfs_spin_unlock(&ras->ras_lock);
440 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
442 struct ll_readahead_state *ras;
446 cfs_spin_lock(&ras->ras_lock);
447 cfs_list_del_init(&rar->lrr_linkage);
448 cfs_spin_unlock(&ras->ras_lock);
451 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
453 struct ll_ra_read *scan;
455 cfs_list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
456 if (scan->lrr_reader == current)
462 struct ll_ra_read *ll_ra_read_get(struct file *f)
464 struct ll_readahead_state *ras;
465 struct ll_ra_read *bead;
469 cfs_spin_lock(&ras->ras_lock);
470 bead = ll_ra_read_get_locked(ras);
471 cfs_spin_unlock(&ras->ras_lock);
475 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
476 struct cl_page_list *queue, struct cl_page *page,
485 cl_page_assume(env, io, page);
486 lu_ref_add(&page->cp_reference, "ra", cfs_current());
487 cp = cl2ccc_page(cl_page_at(page, &vvp_device_type));
488 if (!cp->cpg_defer_uptodate && !PageUptodate(vmpage)) {
489 rc = cl_page_is_under_lock(env, io, page);
491 cp->cpg_defer_uptodate = 1;
493 cl_page_list_add(queue, page);
496 cl_page_delete(env, page);
500 /* skip completed pages */
501 cl_page_unassume(env, io, page);
503 lu_ref_del(&page->cp_reference, "ra", cfs_current());
504 cl_page_put(env, page);
509 * Initiates read-ahead of a page with given index.
511 * \retval +ve: page was added to \a queue.
513 * \retval -ENOLCK: there is no extent lock for this part of a file, stop
516 * \retval -ve, 0: page wasn't added to \a queue for other reason.
518 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
519 struct cl_page_list *queue,
520 pgoff_t index, struct address_space *mapping)
523 struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
524 struct cl_page *page;
525 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
526 unsigned int gfp_mask;
528 const char *msg = NULL;
532 gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
534 gfp_mask |= __GFP_NOWARN;
536 vmpage = grab_cache_page_nowait(mapping, index);
537 if (vmpage != NULL) {
538 /* Check if vmpage was truncated or reclaimed */
539 if (vmpage->mapping == mapping) {
540 page = cl_page_find(env, clob, vmpage->index,
541 vmpage, CPT_CACHEABLE);
543 rc = cl_read_ahead_page(env, io, queue,
546 which = RA_STAT_FAILED_MATCH;
547 msg = "lock match failed";
550 which = RA_STAT_FAILED_GRAB_PAGE;
551 msg = "cl_page_find failed";
554 which = RA_STAT_WRONG_GRAB_PAGE;
555 msg = "g_c_p_n returned invalid page";
559 page_cache_release(vmpage);
561 which = RA_STAT_FAILED_GRAB_PAGE;
562 msg = "g_c_p_n failed";
565 ll_ra_stats_inc(mapping, which);
566 CDEBUG(D_READA, "%s\n", msg);
571 #define RIA_DEBUG(ria) \
572 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
573 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
576 #define RAS_INCREASE_STEP PTLRPC_MAX_BRW_PAGES
578 static inline int stride_io_mode(struct ll_readahead_state *ras)
580 return ras->ras_consecutive_stride_requests > 1;
582 /* The function calculates how much pages will be read in
583 * [off, off + length], in such stride IO area,
584 * stride_offset = st_off, stride_lengh = st_len,
585 * stride_pages = st_pgs
587 * |------------------|*****|------------------|*****|------------|*****|....
590 * |----- st_len -----|
592 * How many pages it should read in such pattern
593 * |-------------------------------------------------------------|
595 * |<------ length ------->|
597 * = |<----->| + |-------------------------------------| + |---|
598 * start_left st_pgs * i end_left
601 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
602 unsigned long off, unsigned long length)
604 __u64 start = off > st_off ? off - st_off : 0;
605 __u64 end = off + length > st_off ? off + length - st_off : 0;
606 unsigned long start_left = 0;
607 unsigned long end_left = 0;
608 unsigned long pg_count;
610 if (st_len == 0 || length == 0 || end == 0)
613 start_left = do_div(start, st_len);
614 if (start_left < st_pgs)
615 start_left = st_pgs - start_left;
619 end_left = do_div(end, st_len);
620 if (end_left > st_pgs)
623 CDEBUG(D_READA, "start "LPU64", end "LPU64" start_left %lu end_left %lu \n",
624 start, end, start_left, end_left);
627 pg_count = end_left - (st_pgs - start_left);
629 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
631 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
632 "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
637 static int ria_page_count(struct ra_io_arg *ria)
639 __u64 length = ria->ria_end >= ria->ria_start ?
640 ria->ria_end - ria->ria_start + 1 : 0;
642 return stride_pg_count(ria->ria_stoff, ria->ria_length,
643 ria->ria_pages, ria->ria_start,
647 /*Check whether the index is in the defined ra-window */
648 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
650 /* If ria_length == ria_pages, it means non-stride I/O mode,
651 * idx should always inside read-ahead window in this case
652 * For stride I/O mode, just check whether the idx is inside
654 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
655 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
656 ria->ria_length < ria->ria_pages);
659 static int ll_read_ahead_pages(const struct lu_env *env,
660 struct cl_io *io, struct cl_page_list *queue,
661 struct ra_io_arg *ria,
662 unsigned long *reserved_pages,
663 struct address_space *mapping,
664 unsigned long *ra_end)
666 int rc, count = 0, stride_ria;
667 unsigned long page_idx;
669 LASSERT(ria != NULL);
672 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
673 for (page_idx = ria->ria_start; page_idx <= ria->ria_end &&
674 *reserved_pages > 0; page_idx++) {
675 if (ras_inside_ra_window(page_idx, ria)) {
676 /* If the page is inside the read-ahead window*/
677 rc = ll_read_ahead_page(env, io, queue,
682 } else if (rc == -ENOLCK)
684 } else if (stride_ria) {
685 /* If it is not in the read-ahead window, and it is
686 * read-ahead mode, then check whether it should skip
689 /* FIXME: This assertion only is valid when it is for
690 * forward read-ahead, it will be fixed when backward
691 * read-ahead is implemented */
692 LASSERTF(page_idx > ria->ria_stoff, "Invalid page_idx %lu"
693 "rs %lu re %lu ro %lu rl %lu rp %lu\n", page_idx,
694 ria->ria_start, ria->ria_end, ria->ria_stoff,
695 ria->ria_length, ria->ria_pages);
696 offset = page_idx - ria->ria_stoff;
697 offset = offset % (ria->ria_length);
698 if (offset > ria->ria_pages) {
699 page_idx += ria->ria_length - offset;
700 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
701 ria->ria_length - offset);
710 int ll_readahead(const struct lu_env *env, struct cl_io *io,
711 struct ll_readahead_state *ras, struct address_space *mapping,
712 struct cl_page_list *queue, int flags)
714 struct vvp_io *vio = vvp_env_io(env);
715 struct vvp_thread_info *vti = vvp_env_info(env);
716 struct cl_attr *attr = ccc_env_thread_attr(env);
717 unsigned long start = 0, end = 0, reserved;
718 unsigned long ra_end, len;
720 struct ll_ra_read *bead;
721 struct ra_io_arg *ria = &vti->vti_ria;
722 struct ll_inode_info *lli;
723 struct cl_object *clob;
728 inode = mapping->host;
729 lli = ll_i2info(inode);
730 clob = lli->lli_clob;
732 memset(ria, 0, sizeof *ria);
734 cl_object_attr_lock(clob);
735 ret = cl_object_attr_get(env, clob, attr);
736 cl_object_attr_unlock(clob);
742 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
746 cfs_spin_lock(&ras->ras_lock);
747 if (vio->cui_ra_window_set)
748 bead = &vio->cui_bead;
752 /* Enlarge the RA window to encompass the full read */
753 if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
754 bead->lrr_start + bead->lrr_count) {
755 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
756 ras->ras_window_start;
758 /* Reserve a part of the read-ahead window that we'll be issuing */
759 if (ras->ras_window_len) {
760 start = ras->ras_next_readahead;
761 end = ras->ras_window_start + ras->ras_window_len - 1;
764 unsigned long rpc_boundary;
766 * Align RA window to an optimal boundary.
768 * XXX This would be better to align to cl_max_pages_per_rpc
769 * instead of PTLRPC_MAX_BRW_PAGES, because the RPC size may
770 * be aligned to the RAID stripe size in the future and that
771 * is more important than the RPC size.
773 /* Note: we only trim the RPC, instead of extending the RPC
774 * to the boundary, so to avoid reading too much pages during
776 rpc_boundary = ((end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1)));
777 if (rpc_boundary > 0)
780 if (rpc_boundary > start)
783 /* Truncate RA window to end of file */
784 end = min(end, (unsigned long)((kms - 1) >> CFS_PAGE_SHIFT));
786 ras->ras_next_readahead = max(end, end + 1);
789 ria->ria_start = start;
791 /* If stride I/O mode is detected, get stride window*/
792 if (stride_io_mode(ras)) {
793 ria->ria_stoff = ras->ras_stride_offset;
794 ria->ria_length = ras->ras_stride_length;
795 ria->ria_pages = ras->ras_stride_pages;
797 cfs_spin_unlock(&ras->ras_lock);
800 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
803 len = ria_page_count(ria);
807 reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len);
809 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
811 CDEBUG(D_READA, "reserved page %lu ra_cur %d ra_max %lu\n", reserved,
812 cfs_atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
813 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
815 ret = ll_read_ahead_pages(env, io, queue,
816 ria, &reserved, mapping, &ra_end);
818 LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
820 ll_ra_count_put(ll_i2sbi(inode), reserved);
822 if (ra_end == end + 1 && ra_end == (kms >> CFS_PAGE_SHIFT))
823 ll_ra_stats_inc(mapping, RA_STAT_EOF);
825 /* if we didn't get to the end of the region we reserved from
826 * the ras we need to go back and update the ras so that the
827 * next read-ahead tries from where we left off. we only do so
828 * if the region we failed to issue read-ahead on is still ahead
829 * of the app and behind the next index to start read-ahead from */
830 CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
831 ra_end, end, ria->ria_end);
833 if (ra_end != end + 1) {
834 cfs_spin_lock(&ras->ras_lock);
835 if (ra_end < ras->ras_next_readahead &&
836 index_in_window(ra_end, ras->ras_window_start, 0,
837 ras->ras_window_len)) {
838 ras->ras_next_readahead = ra_end;
841 cfs_spin_unlock(&ras->ras_lock);
847 static void ras_set_start(struct ll_readahead_state *ras, unsigned long index)
849 ras->ras_window_start = index & (~(RAS_INCREASE_STEP - 1));
852 /* called with the ras_lock held or from places where it doesn't matter */
853 static void ras_reset(struct ll_readahead_state *ras, unsigned long index)
855 ras->ras_last_readpage = index;
856 ras->ras_consecutive_requests = 0;
857 ras->ras_consecutive_pages = 0;
858 ras->ras_window_len = 0;
859 ras_set_start(ras, index);
860 ras->ras_next_readahead = max(ras->ras_window_start, index);
865 /* called with the ras_lock held or from places where it doesn't matter */
866 static void ras_stride_reset(struct ll_readahead_state *ras)
868 ras->ras_consecutive_stride_requests = 0;
869 ras->ras_stride_length = 0;
870 ras->ras_stride_pages = 0;
874 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
876 cfs_spin_lock_init(&ras->ras_lock);
878 ras->ras_requests = 0;
879 CFS_INIT_LIST_HEAD(&ras->ras_read_beads);
883 * Check whether the read request is in the stride window.
884 * If it is in the stride window, return 1, otherwise return 0.
886 static int index_in_stride_window(unsigned long index,
887 struct ll_readahead_state *ras,
890 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
892 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
893 ras->ras_stride_pages == ras->ras_stride_length)
896 /* If it is contiguous read */
898 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
900 /*Otherwise check the stride by itself */
901 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
902 ras->ras_consecutive_pages == ras->ras_stride_pages;
905 static void ras_update_stride_detector(struct ll_readahead_state *ras,
908 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
910 if (!stride_io_mode(ras) && (stride_gap != 0 ||
911 ras->ras_consecutive_stride_requests == 0)) {
912 ras->ras_stride_pages = ras->ras_consecutive_pages;
913 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
915 LASSERT(ras->ras_request_index == 0);
916 LASSERT(ras->ras_consecutive_stride_requests == 0);
918 if (index <= ras->ras_last_readpage) {
919 /*Reset stride window for forward read*/
920 ras_stride_reset(ras);
924 ras->ras_stride_pages = ras->ras_consecutive_pages;
925 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
932 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
934 return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
935 ras->ras_stride_pages, ras->ras_stride_offset,
939 /* Stride Read-ahead window will be increased inc_len according to
940 * stride I/O pattern */
941 static void ras_stride_increase_window(struct ll_readahead_state *ras,
942 struct ll_ra_info *ra,
943 unsigned long inc_len)
945 unsigned long left, step, window_len;
946 unsigned long stride_len;
948 LASSERT(ras->ras_stride_length > 0);
949 LASSERTF(ras->ras_window_start + ras->ras_window_len
950 >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
951 " stride_offset %lu\n", ras->ras_window_start,
952 ras->ras_window_len, ras->ras_stride_offset);
954 stride_len = ras->ras_window_start + ras->ras_window_len -
955 ras->ras_stride_offset;
957 left = stride_len % ras->ras_stride_length;
958 window_len = ras->ras_window_len - left;
960 if (left < ras->ras_stride_pages)
963 left = ras->ras_stride_pages + inc_len;
965 LASSERT(ras->ras_stride_pages != 0);
967 step = left / ras->ras_stride_pages;
968 left %= ras->ras_stride_pages;
970 window_len += step * ras->ras_stride_length + left;
972 if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
973 ras->ras_window_len = window_len;
978 static void ras_increase_window(struct ll_readahead_state *ras,
979 struct ll_ra_info *ra, struct inode *inode)
981 /* The stretch of ra-window should be aligned with max rpc_size
982 * but current clio architecture does not support retrieve such
983 * information from lower layer. FIXME later
985 if (stride_io_mode(ras))
986 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP);
988 ras->ras_window_len = min(ras->ras_window_len +
990 ra->ra_max_pages_per_file);
993 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
994 struct ll_readahead_state *ras, unsigned long index,
997 struct ll_ra_info *ra = &sbi->ll_ra_info;
998 int zero = 0, stride_detect = 0, ra_miss = 0;
1001 cfs_spin_lock(&ras->ras_lock);
1003 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
1005 /* reset the read-ahead window in two cases. First when the app seeks
1006 * or reads to some other part of the file. Secondly if we get a
1007 * read-ahead miss that we think we've previously issued. This can
1008 * be a symptom of there being so many read-ahead pages that the VM is
1009 * reclaiming it before we get to it. */
1010 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
1012 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
1013 } else if (!hit && ras->ras_window_len &&
1014 index < ras->ras_next_readahead &&
1015 index_in_window(index, ras->ras_window_start, 0,
1016 ras->ras_window_len)) {
1018 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
1021 /* On the second access to a file smaller than the tunable
1022 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
1023 * file up to ra_max_pages_per_file. This is simply a best effort
1024 * and only occurs once per open file. Normal RA behavior is reverted
1025 * to for subsequent IO. The mmap case does not increment
1026 * ras_requests and thus can never trigger this behavior. */
1027 if (ras->ras_requests == 2 && !ras->ras_request_index) {
1030 kms_pages = (i_size_read(inode) + CFS_PAGE_SIZE - 1) >>
1033 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
1034 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
1037 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
1038 ras->ras_window_start = 0;
1039 ras->ras_last_readpage = 0;
1040 ras->ras_next_readahead = 0;
1041 ras->ras_window_len = min(ra->ra_max_pages_per_file,
1042 ra->ra_max_read_ahead_whole_pages);
1043 GOTO(out_unlock, 0);
1047 /* check whether it is in stride I/O mode*/
1048 if (!index_in_stride_window(index, ras, inode)) {
1049 if (ras->ras_consecutive_stride_requests == 0 &&
1050 ras->ras_request_index == 0) {
1051 ras_update_stride_detector(ras, index);
1052 ras->ras_consecutive_stride_requests ++;
1054 ras_stride_reset(ras);
1056 ras_reset(ras, index);
1057 ras->ras_consecutive_pages++;
1058 GOTO(out_unlock, 0);
1060 ras->ras_consecutive_pages = 0;
1061 ras->ras_consecutive_requests = 0;
1062 if (++ras->ras_consecutive_stride_requests > 1)
1068 if (index_in_stride_window(index, ras, inode) &&
1069 stride_io_mode(ras)) {
1070 /*If stride-RA hit cache miss, the stride dector
1071 *will not be reset to avoid the overhead of
1072 *redetecting read-ahead mode */
1073 if (index != ras->ras_last_readpage + 1)
1074 ras->ras_consecutive_pages = 0;
1075 ras_reset(ras, index);
1078 /* Reset both stride window and normal RA
1080 ras_reset(ras, index);
1081 ras->ras_consecutive_pages++;
1082 ras_stride_reset(ras);
1083 GOTO(out_unlock, 0);
1085 } else if (stride_io_mode(ras)) {
1086 /* If this is contiguous read but in stride I/O mode
1087 * currently, check whether stride step still is valid,
1088 * if invalid, it will reset the stride ra window*/
1089 if (!index_in_stride_window(index, ras, inode)) {
1090 /* Shrink stride read-ahead window to be zero */
1091 ras_stride_reset(ras);
1092 ras->ras_window_len = 0;
1093 ras->ras_next_readahead = index;
1097 ras->ras_consecutive_pages++;
1098 ras->ras_last_readpage = index;
1099 ras_set_start(ras, index);
1101 if (stride_io_mode(ras))
1102 /* Since stride readahead is sentivite to the offset
1103 * of read-ahead, so we use original offset here,
1104 * instead of ras_window_start, which is 1M aligned*/
1105 ras->ras_next_readahead = max(index,
1106 ras->ras_next_readahead);
1108 ras->ras_next_readahead = max(ras->ras_window_start,
1109 ras->ras_next_readahead);
1112 /* Trigger RA in the mmap case where ras_consecutive_requests
1113 * is not incremented and thus can't be used to trigger RA */
1114 if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
1115 ras->ras_window_len = RAS_INCREASE_STEP;
1116 GOTO(out_unlock, 0);
1119 /* Initially reset the stride window offset to next_readahead*/
1120 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
1122 * Once stride IO mode is detected, next_readahead should be
1123 * reset to make sure next_readahead > stride offset
1125 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
1126 ras->ras_stride_offset = index;
1127 ras->ras_window_len = RAS_INCREASE_STEP;
1130 /* The initial ras_window_len is set to the request size. To avoid
1131 * uselessly reading and discarding pages for random IO the window is
1132 * only increased once per consecutive request received. */
1133 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
1134 !ras->ras_request_index)
1135 ras_increase_window(ras, ra, inode);
1139 ras->ras_request_index++;
1140 cfs_spin_unlock(&ras->ras_lock);
1144 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1146 struct inode *inode = vmpage->mapping->host;
1147 struct ll_inode_info *lli = ll_i2info(inode);
1150 struct cl_page *page;
1151 struct cl_object *clob;
1152 struct cl_env_nest nest;
1153 bool redirtied = false;
1154 bool unlocked = false;
1158 LASSERT(PageLocked(vmpage));
1159 LASSERT(!PageWriteback(vmpage));
1161 LASSERT(ll_i2dtexp(inode) != NULL);
1163 env = cl_env_nested_get(&nest);
1165 GOTO(out, result = PTR_ERR(env));
1167 clob = ll_i2info(inode)->lli_clob;
1168 LASSERT(clob != NULL);
1170 io = ccc_env_thread_io(env);
1172 io->ci_ignore_layout = 1;
1173 result = cl_io_init(env, io, CIT_MISC, clob);
1175 page = cl_page_find(env, clob, vmpage->index,
1176 vmpage, CPT_CACHEABLE);
1177 if (!IS_ERR(page)) {
1178 lu_ref_add(&page->cp_reference, "writepage",
1180 cl_page_assume(env, io, page);
1181 result = cl_page_flush(env, io, page);
1184 * Re-dirty page on error so it retries write,
1185 * but not in case when IO has actually
1186 * occurred and completed with an error.
1188 if (!PageError(vmpage)) {
1189 redirty_page_for_writepage(wbc, vmpage);
1194 cl_page_disown(env, io, page);
1196 lu_ref_del(&page->cp_reference,
1197 "writepage", cfs_current());
1198 cl_page_put(env, page);
1200 result = PTR_ERR(page);
1203 cl_io_fini(env, io);
1205 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1206 loff_t offset = cl_offset(clob, vmpage->index);
1208 /* Flush page failed because the extent is being written out.
1209 * Wait for the write of extent to be finished to avoid
1210 * breaking kernel which assumes ->writepage should mark
1211 * PageWriteback or clean the page. */
1212 result = cl_sync_file_range(inode, offset,
1213 offset + CFS_PAGE_SIZE - 1,
1216 /* actually we may have written more than one page.
1217 * decreasing this page because the caller will count
1219 wbc->nr_to_write -= result - 1;
1224 cl_env_nested_put(&nest, env);
1229 if (!lli->lli_async_rc)
1230 lli->lli_async_rc = result;
1231 SetPageError(vmpage);
1233 unlock_page(vmpage);
1238 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1240 struct inode *inode = mapping->host;
1243 enum cl_fsync_mode mode;
1244 int range_whole = 0;
1248 if (wbc->range_cyclic) {
1249 start = mapping->writeback_index << CFS_PAGE_SHIFT;
1250 end = OBD_OBJECT_EOF;
1252 start = wbc->range_start;
1253 end = wbc->range_end;
1254 if (end == LLONG_MAX) {
1255 end = OBD_OBJECT_EOF;
1256 range_whole = start == 0;
1260 mode = CL_FSYNC_NONE;
1261 if (wbc->sync_mode == WB_SYNC_ALL)
1262 mode = CL_FSYNC_LOCAL;
1264 result = cl_sync_file_range(inode, start, end, mode);
1266 wbc->nr_to_write -= result;
1270 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1271 if (end == OBD_OBJECT_EOF)
1272 end = i_size_read(inode);
1273 mapping->writeback_index = (end >> CFS_PAGE_SHIFT) + 1;
1278 int ll_readpage(struct file *file, struct page *vmpage)
1280 struct ll_cl_context *lcc;
1284 lcc = ll_cl_init(file, vmpage, 0);
1286 struct lu_env *env = lcc->lcc_env;
1287 struct cl_io *io = lcc->lcc_io;
1288 struct cl_page *page = lcc->lcc_page;
1290 LASSERT(page->cp_type == CPT_CACHEABLE);
1291 if (likely(!PageUptodate(vmpage))) {
1292 cl_page_assume(env, io, page);
1293 result = cl_io_read_page(env, io, page);
1295 /* Page from a non-object file. */
1296 LASSERT(!ll_i2info(vmpage->mapping->host)->lli_has_smd);
1297 unlock_page(vmpage);
1302 unlock_page(vmpage);
1303 result = PTR_ERR(lcc);