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, Intel Corporation.
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/uaccess.h>
51 #include <linux/stat.h>
52 #include <asm/uaccess.h>
54 #include <linux/pagemap.h>
55 /* current_is_kswapd() */
56 #include <linux/swap.h>
58 #define DEBUG_SUBSYSTEM S_LLITE
60 #include <lustre_lite.h>
61 #include <obd_cksum.h>
62 #include "llite_internal.h"
63 #include <linux/lustre_compat25.h>
66 * Finalizes cl-data before exiting typical address_space operation. Dual to
69 static void ll_cl_fini(struct ll_cl_context *lcc)
71 struct lu_env *env = lcc->lcc_env;
72 struct cl_io *io = lcc->lcc_io;
73 struct cl_page *page = lcc->lcc_page;
75 LASSERT(lcc->lcc_cookie == current);
79 lu_ref_del(&page->cp_reference, "cl_io", io);
80 cl_page_put(env, page);
83 if (io && lcc->lcc_created) {
85 cl_io_unlock(env, io);
86 cl_io_iter_fini(env, io);
89 cl_env_put(env, &lcc->lcc_refcheck);
93 * Initializes common cl-data at the typical address_space operation entry
96 static struct ll_cl_context *ll_cl_init(struct file *file,
97 struct page *vmpage, int create)
99 struct ll_cl_context *lcc;
102 struct cl_object *clob;
108 clob = ll_i2info(vmpage->mapping->host)->lli_clob;
109 LASSERT(clob != NULL);
111 env = cl_env_get(&refcheck);
113 return ERR_PTR(PTR_ERR(env));
115 lcc = &vvp_env_info(env)->vti_io_ctx;
116 memset(lcc, 0, sizeof(*lcc));
118 lcc->lcc_refcheck = refcheck;
119 lcc->lcc_cookie = current;
121 cio = ccc_env_io(env);
122 io = cio->cui_cl.cis_io;
123 if (io == NULL && create) {
124 struct inode *inode = vmpage->mapping->host;
127 if (mutex_trylock(&inode->i_mutex)) {
128 mutex_unlock(&(inode)->i_mutex);
130 /* this is too bad. Someone is trying to write the
131 * page w/o holding inode mutex. This means we can
132 * add dirty pages into cache during truncate */
133 CERROR("Proc %s is dirting page w/o inode lock, this"
134 "will break truncate.\n", cfs_current()->comm);
135 libcfs_debug_dumpstack(NULL);
137 return ERR_PTR(-EIO);
141 * Loop-back driver calls ->prepare_write() and ->sendfile()
142 * methods directly, bypassing file system ->write() operation,
143 * so cl_io has to be created here.
145 io = ccc_env_thread_io(env);
146 ll_io_init(io, file, 1);
148 /* No lock at all for this kind of IO - we can't do it because
149 * we have held page lock, it would cause deadlock.
150 * XXX: This causes poor performance to loop device - One page
152 * In order to get better performance, users should use
153 * lloop driver instead.
155 io->ci_lockreq = CILR_NEVER;
157 pos = (vmpage->index << CFS_PAGE_SHIFT);
159 /* Create a temp IO to serve write. */
160 result = cl_io_rw_init(env, io, CIT_WRITE, pos, CFS_PAGE_SIZE);
162 cio->cui_fd = LUSTRE_FPRIVATE(file);
165 result = cl_io_iter_init(env, io);
167 result = cl_io_lock(env, io);
169 result = cl_io_start(env, io);
172 result = io->ci_result;
173 lcc->lcc_created = 1;
180 struct cl_page *page;
183 LASSERT(io->ci_state == CIS_IO_GOING);
184 LASSERT(cio->cui_fd == LUSTRE_FPRIVATE(file));
185 page = cl_page_find(env, clob, vmpage->index, vmpage,
188 lcc->lcc_page = page;
189 lu_ref_add(&page->cp_reference, "cl_io", io);
192 result = PTR_ERR(page);
196 lcc = ERR_PTR(result);
199 CDEBUG(D_VFSTRACE, "%lu@"DFID" -> %d %p %p\n",
200 vmpage->index, PFID(lu_object_fid(&clob->co_lu)), result,
205 static struct ll_cl_context *ll_cl_get(void)
207 struct ll_cl_context *lcc;
211 env = cl_env_get(&refcheck);
212 LASSERT(!IS_ERR(env));
213 lcc = &vvp_env_info(env)->vti_io_ctx;
214 LASSERT(env == lcc->lcc_env);
215 LASSERT(current == lcc->lcc_cookie);
216 cl_env_put(env, &refcheck);
218 /* env has got in ll_cl_init, so it is still usable. */
223 * ->prepare_write() address space operation called by generic_file_write()
224 * for every page during write.
226 int ll_prepare_write(struct file *file, struct page *vmpage, unsigned from,
229 struct ll_cl_context *lcc;
233 lcc = ll_cl_init(file, vmpage, 1);
235 struct lu_env *env = lcc->lcc_env;
236 struct cl_io *io = lcc->lcc_io;
237 struct cl_page *page = lcc->lcc_page;
239 cl_page_assume(env, io, page);
240 if (cl_io_is_append(io)) {
241 struct cl_object *obj = io->ci_obj;
242 struct inode *inode = ccc_object_inode(obj);
244 * In VFS file->page write loop, for appending, the
245 * write offset might be reset according to the new
246 * file size before holding i_mutex. So crw_pos should
247 * be reset here. BUG:17711.
249 io->u.ci_wr.wr.crw_pos = i_size_read(inode);
251 result = cl_io_prepare_write(env, io, page, from, to);
254 * Add a reference, so that page is not evicted from
255 * the cache until ->commit_write() is called.
258 lu_ref_add(&page->cp_reference, "prepare_write",
261 cl_page_unassume(env, io, page);
264 /* returning 0 in prepare assumes commit must be called
267 result = PTR_ERR(lcc);
272 int ll_commit_write(struct file *file, struct page *vmpage, unsigned from,
275 struct ll_cl_context *lcc;
278 struct cl_page *page;
284 page = lcc->lcc_page;
287 LASSERT(cl_page_is_owned(page, io));
289 if (from != to) /* handle short write case. */
290 result = cl_io_commit_write(env, io, page, from, to);
291 if (cl_page_is_owned(page, io))
292 cl_page_unassume(env, io, page);
295 * Release reference acquired by ll_prepare_write().
297 lu_ref_del(&page->cp_reference, "prepare_write", cfs_current());
298 cl_page_put(env, page);
303 struct obd_capa *cl_capa_lookup(struct inode *inode, enum cl_req_type crt)
307 opc = crt == CRT_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
308 return ll_osscapa_get(inode, opc);
311 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
314 * Get readahead pages from the filesystem readahead pool of the client for a
317 * /param sbi superblock for filesystem readahead state ll_ra_info
318 * /param ria per-thread readahead state
319 * /param pages number of pages requested for readahead for the thread.
321 * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
322 * It should work well if the ra_max_pages is much greater than the single
323 * file's read-ahead window, and not too many threads contending for
324 * these readahead pages.
326 * TODO: There may be a 'global sync problem' if many threads are trying
327 * to get an ra budget that is larger than the remaining readahead pages
328 * and reach here at exactly the same time. They will compute /a ret to
329 * consume the remaining pages, but will fail at atomic_add_return() and
330 * get a zero ra window, although there is still ra space remaining. - Jay */
332 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
333 struct ra_io_arg *ria,
336 struct ll_ra_info *ra = &sbi->ll_ra_info;
340 /* If read-ahead pages left are less than 1M, do not do read-ahead,
341 * otherwise it will form small read RPC(< 1M), which hurt server
342 * performance a lot. */
343 ret = min(ra->ra_max_pages - cfs_atomic_read(&ra->ra_cur_pages), pages);
344 if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages))
347 /* If the non-strided (ria_pages == 0) readahead window
348 * (ria_start + ret) has grown across an RPC boundary, then trim
349 * readahead size by the amount beyond the RPC so it ends on an
350 * RPC boundary. If the readahead window is already ending on
351 * an RPC boundary (beyond_rpc == 0), or smaller than a full
352 * RPC (beyond_rpc < ret) the readahead size is unchanged.
353 * The (beyond_rpc != 0) check is skipped since the conditional
354 * branch is more expensive than subtracting zero from the result.
356 * Strided read is left unaligned to avoid small fragments beyond
357 * the RPC boundary from needing an extra read RPC. */
358 if (ria->ria_pages == 0) {
359 long beyond_rpc = (ria->ria_start + ret) % PTLRPC_MAX_BRW_PAGES;
360 if (/* beyond_rpc != 0 && */ beyond_rpc < ret)
364 if (cfs_atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
365 cfs_atomic_sub(ret, &ra->ra_cur_pages);
373 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
375 struct ll_ra_info *ra = &sbi->ll_ra_info;
376 cfs_atomic_sub(len, &ra->ra_cur_pages);
379 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
381 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
382 lprocfs_counter_incr(sbi->ll_ra_stats, which);
385 void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
387 struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
388 ll_ra_stats_inc_sbi(sbi, which);
391 #define RAS_CDEBUG(ras) \
393 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu" \
394 "csr %lu sf %lu sp %lu sl %lu \n", \
395 ras->ras_last_readpage, ras->ras_consecutive_requests, \
396 ras->ras_consecutive_pages, ras->ras_window_start, \
397 ras->ras_window_len, ras->ras_next_readahead, \
398 ras->ras_requests, ras->ras_request_index, \
399 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
400 ras->ras_stride_pages, ras->ras_stride_length)
402 static int index_in_window(unsigned long index, unsigned long point,
403 unsigned long before, unsigned long after)
405 unsigned long start = point - before, end = point + after;
412 return start <= index && index <= end;
415 static struct ll_readahead_state *ll_ras_get(struct file *f)
417 struct ll_file_data *fd;
419 fd = LUSTRE_FPRIVATE(f);
423 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar)
425 struct ll_readahead_state *ras;
429 spin_lock(&ras->ras_lock);
431 ras->ras_request_index = 0;
432 ras->ras_consecutive_requests++;
433 rar->lrr_reader = current;
435 cfs_list_add(&rar->lrr_linkage, &ras->ras_read_beads);
436 spin_unlock(&ras->ras_lock);
439 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
441 struct ll_readahead_state *ras;
445 spin_lock(&ras->ras_lock);
446 cfs_list_del_init(&rar->lrr_linkage);
447 spin_unlock(&ras->ras_lock);
450 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
452 struct ll_ra_read *scan;
454 cfs_list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
455 if (scan->lrr_reader == current)
461 struct ll_ra_read *ll_ra_read_get(struct file *f)
463 struct ll_readahead_state *ras;
464 struct ll_ra_read *bead;
468 spin_lock(&ras->ras_lock);
469 bead = ll_ra_read_get_locked(ras);
470 spin_unlock(&ras->ras_lock);
474 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
475 struct cl_page_list *queue, struct cl_page *page,
484 cl_page_assume(env, io, page);
485 lu_ref_add(&page->cp_reference, "ra", cfs_current());
486 cp = cl2ccc_page(cl_page_at(page, &vvp_device_type));
487 if (!cp->cpg_defer_uptodate && !PageUptodate(vmpage)) {
488 rc = cl_page_is_under_lock(env, io, page);
490 cp->cpg_defer_uptodate = 1;
492 cl_page_list_add(queue, page);
495 cl_page_delete(env, page);
499 /* skip completed pages */
500 cl_page_unassume(env, io, page);
502 lu_ref_del(&page->cp_reference, "ra", cfs_current());
503 cl_page_put(env, page);
508 * Initiates read-ahead of a page with given index.
510 * \retval +ve: page was added to \a queue.
512 * \retval -ENOLCK: there is no extent lock for this part of a file, stop
515 * \retval -ve, 0: page wasn't added to \a queue for other reason.
517 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
518 struct cl_page_list *queue,
519 pgoff_t index, struct address_space *mapping)
522 struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
523 struct cl_page *page;
524 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
525 unsigned int gfp_mask;
527 const char *msg = NULL;
531 gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
533 gfp_mask |= __GFP_NOWARN;
535 vmpage = grab_cache_page_nowait(mapping, index);
536 if (vmpage != NULL) {
537 /* Check if vmpage was truncated or reclaimed */
538 if (vmpage->mapping == mapping) {
539 page = cl_page_find(env, clob, vmpage->index,
540 vmpage, CPT_CACHEABLE);
542 rc = cl_read_ahead_page(env, io, queue,
545 which = RA_STAT_FAILED_MATCH;
546 msg = "lock match failed";
549 which = RA_STAT_FAILED_GRAB_PAGE;
550 msg = "cl_page_find failed";
553 which = RA_STAT_WRONG_GRAB_PAGE;
554 msg = "g_c_p_n returned invalid page";
558 page_cache_release(vmpage);
560 which = RA_STAT_FAILED_GRAB_PAGE;
561 msg = "g_c_p_n failed";
564 ll_ra_stats_inc(mapping, which);
565 CDEBUG(D_READA, "%s\n", msg);
570 #define RIA_DEBUG(ria) \
571 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
572 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
575 /* Limit this to the blocksize instead of PTLRPC_BRW_MAX_SIZE, since we don't
576 * know what the actual RPC size is. If this needs to change, it makes more
577 * sense to tune the i_blkbits value for the file based on the OSTs it is
578 * striped over, rather than having a constant value for all files here. */
580 /* RAS_INCREASE_STEP should be (1UL << (inode->i_blkbits - CFS_PAGE_SHIFT)).
581 * Temprarily set RAS_INCREASE_STEP to 1MB. After 4MB RPC is enabled
582 * by default, this should be adjusted corresponding with max_read_ahead_mb
583 * and max_read_ahead_per_file_mb otherwise the readahead budget can be used
584 * up quickly which will affect read performance siginificantly. See LU-2816 */
585 #define RAS_INCREASE_STEP(inode) (ONE_MB_BRW_SIZE >> CFS_PAGE_SHIFT)
587 static inline int stride_io_mode(struct ll_readahead_state *ras)
589 return ras->ras_consecutive_stride_requests > 1;
591 /* The function calculates how much pages will be read in
592 * [off, off + length], in such stride IO area,
593 * stride_offset = st_off, stride_lengh = st_len,
594 * stride_pages = st_pgs
596 * |------------------|*****|------------------|*****|------------|*****|....
599 * |----- st_len -----|
601 * How many pages it should read in such pattern
602 * |-------------------------------------------------------------|
604 * |<------ length ------->|
606 * = |<----->| + |-------------------------------------| + |---|
607 * start_left st_pgs * i end_left
610 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
611 unsigned long off, unsigned long length)
613 __u64 start = off > st_off ? off - st_off : 0;
614 __u64 end = off + length > st_off ? off + length - st_off : 0;
615 unsigned long start_left = 0;
616 unsigned long end_left = 0;
617 unsigned long pg_count;
619 if (st_len == 0 || length == 0 || end == 0)
622 start_left = do_div(start, st_len);
623 if (start_left < st_pgs)
624 start_left = st_pgs - start_left;
628 end_left = do_div(end, st_len);
629 if (end_left > st_pgs)
632 CDEBUG(D_READA, "start "LPU64", end "LPU64" start_left %lu end_left %lu \n",
633 start, end, start_left, end_left);
636 pg_count = end_left - (st_pgs - start_left);
638 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
640 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
641 "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
646 static int ria_page_count(struct ra_io_arg *ria)
648 __u64 length = ria->ria_end >= ria->ria_start ?
649 ria->ria_end - ria->ria_start + 1 : 0;
651 return stride_pg_count(ria->ria_stoff, ria->ria_length,
652 ria->ria_pages, ria->ria_start,
656 /*Check whether the index is in the defined ra-window */
657 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
659 /* If ria_length == ria_pages, it means non-stride I/O mode,
660 * idx should always inside read-ahead window in this case
661 * For stride I/O mode, just check whether the idx is inside
663 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
664 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
665 ria->ria_length < ria->ria_pages);
668 static int ll_read_ahead_pages(const struct lu_env *env,
669 struct cl_io *io, struct cl_page_list *queue,
670 struct ra_io_arg *ria,
671 unsigned long *reserved_pages,
672 struct address_space *mapping,
673 unsigned long *ra_end)
675 int rc, count = 0, stride_ria;
676 unsigned long page_idx;
678 LASSERT(ria != NULL);
681 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
682 for (page_idx = ria->ria_start; page_idx <= ria->ria_end &&
683 *reserved_pages > 0; page_idx++) {
684 if (ras_inside_ra_window(page_idx, ria)) {
685 /* If the page is inside the read-ahead window*/
686 rc = ll_read_ahead_page(env, io, queue,
691 } else if (rc == -ENOLCK)
693 } else if (stride_ria) {
694 /* If it is not in the read-ahead window, and it is
695 * read-ahead mode, then check whether it should skip
698 /* FIXME: This assertion only is valid when it is for
699 * forward read-ahead, it will be fixed when backward
700 * read-ahead is implemented */
701 LASSERTF(page_idx > ria->ria_stoff, "Invalid page_idx %lu"
702 "rs %lu re %lu ro %lu rl %lu rp %lu\n", page_idx,
703 ria->ria_start, ria->ria_end, ria->ria_stoff,
704 ria->ria_length, ria->ria_pages);
705 offset = page_idx - ria->ria_stoff;
706 offset = offset % (ria->ria_length);
707 if (offset > ria->ria_pages) {
708 page_idx += ria->ria_length - offset;
709 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
710 ria->ria_length - offset);
719 int ll_readahead(const struct lu_env *env, struct cl_io *io,
720 struct ll_readahead_state *ras, struct address_space *mapping,
721 struct cl_page_list *queue, int flags)
723 struct vvp_io *vio = vvp_env_io(env);
724 struct vvp_thread_info *vti = vvp_env_info(env);
725 struct cl_attr *attr = ccc_env_thread_attr(env);
726 unsigned long start = 0, end = 0, reserved;
727 unsigned long ra_end, len;
729 struct ll_ra_read *bead;
730 struct ra_io_arg *ria = &vti->vti_ria;
731 struct ll_inode_info *lli;
732 struct cl_object *clob;
737 inode = mapping->host;
738 lli = ll_i2info(inode);
739 clob = lli->lli_clob;
741 memset(ria, 0, sizeof *ria);
743 cl_object_attr_lock(clob);
744 ret = cl_object_attr_get(env, clob, attr);
745 cl_object_attr_unlock(clob);
751 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
755 spin_lock(&ras->ras_lock);
756 if (vio->cui_ra_window_set)
757 bead = &vio->cui_bead;
761 /* Enlarge the RA window to encompass the full read */
762 if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
763 bead->lrr_start + bead->lrr_count) {
764 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
765 ras->ras_window_start;
767 /* Reserve a part of the read-ahead window that we'll be issuing */
768 if (ras->ras_window_len) {
769 start = ras->ras_next_readahead;
770 end = ras->ras_window_start + ras->ras_window_len - 1;
773 unsigned long rpc_boundary;
775 * Align RA window to an optimal boundary.
777 * XXX This would be better to align to cl_max_pages_per_rpc
778 * instead of PTLRPC_MAX_BRW_PAGES, because the RPC size may
779 * be aligned to the RAID stripe size in the future and that
780 * is more important than the RPC size.
782 /* Note: we only trim the RPC, instead of extending the RPC
783 * to the boundary, so to avoid reading too much pages during
785 rpc_boundary = ((end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1)));
786 if (rpc_boundary > 0)
789 if (rpc_boundary > start)
792 /* Truncate RA window to end of file */
793 end = min(end, (unsigned long)((kms - 1) >> CFS_PAGE_SHIFT));
795 ras->ras_next_readahead = max(end, end + 1);
798 ria->ria_start = start;
800 /* If stride I/O mode is detected, get stride window*/
801 if (stride_io_mode(ras)) {
802 ria->ria_stoff = ras->ras_stride_offset;
803 ria->ria_length = ras->ras_stride_length;
804 ria->ria_pages = ras->ras_stride_pages;
806 spin_unlock(&ras->ras_lock);
809 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
812 len = ria_page_count(ria);
816 reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len);
818 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
820 CDEBUG(D_READA, "reserved page %lu ra_cur %d ra_max %lu\n", reserved,
821 cfs_atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
822 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
824 ret = ll_read_ahead_pages(env, io, queue,
825 ria, &reserved, mapping, &ra_end);
827 LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
829 ll_ra_count_put(ll_i2sbi(inode), reserved);
831 if (ra_end == end + 1 && ra_end == (kms >> CFS_PAGE_SHIFT))
832 ll_ra_stats_inc(mapping, RA_STAT_EOF);
834 /* if we didn't get to the end of the region we reserved from
835 * the ras we need to go back and update the ras so that the
836 * next read-ahead tries from where we left off. we only do so
837 * if the region we failed to issue read-ahead on is still ahead
838 * of the app and behind the next index to start read-ahead from */
839 CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
840 ra_end, end, ria->ria_end);
842 if (ra_end != end + 1) {
843 spin_lock(&ras->ras_lock);
844 if (ra_end < ras->ras_next_readahead &&
845 index_in_window(ra_end, ras->ras_window_start, 0,
846 ras->ras_window_len)) {
847 ras->ras_next_readahead = ra_end;
850 spin_unlock(&ras->ras_lock);
856 static void ras_set_start(struct inode *inode, struct ll_readahead_state *ras,
859 ras->ras_window_start = index & (~(RAS_INCREASE_STEP(inode) - 1));
862 /* called with the ras_lock held or from places where it doesn't matter */
863 static void ras_reset(struct inode *inode, struct ll_readahead_state *ras,
866 ras->ras_last_readpage = index;
867 ras->ras_consecutive_requests = 0;
868 ras->ras_consecutive_pages = 0;
869 ras->ras_window_len = 0;
870 ras_set_start(inode, ras, index);
871 ras->ras_next_readahead = max(ras->ras_window_start, index);
876 /* called with the ras_lock held or from places where it doesn't matter */
877 static void ras_stride_reset(struct ll_readahead_state *ras)
879 ras->ras_consecutive_stride_requests = 0;
880 ras->ras_stride_length = 0;
881 ras->ras_stride_pages = 0;
885 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
887 spin_lock_init(&ras->ras_lock);
888 ras_reset(inode, ras, 0);
889 ras->ras_requests = 0;
890 CFS_INIT_LIST_HEAD(&ras->ras_read_beads);
894 * Check whether the read request is in the stride window.
895 * If it is in the stride window, return 1, otherwise return 0.
897 static int index_in_stride_window(struct ll_readahead_state *ras,
900 unsigned long stride_gap;
902 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
903 ras->ras_stride_pages == ras->ras_stride_length)
906 stride_gap = index - ras->ras_last_readpage - 1;
908 /* If it is contiguous read */
910 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
912 /* Otherwise check the stride by itself */
913 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
914 ras->ras_consecutive_pages == ras->ras_stride_pages;
917 static void ras_update_stride_detector(struct ll_readahead_state *ras,
920 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
922 if (!stride_io_mode(ras) && (stride_gap != 0 ||
923 ras->ras_consecutive_stride_requests == 0)) {
924 ras->ras_stride_pages = ras->ras_consecutive_pages;
925 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
927 LASSERT(ras->ras_request_index == 0);
928 LASSERT(ras->ras_consecutive_stride_requests == 0);
930 if (index <= ras->ras_last_readpage) {
931 /*Reset stride window for forward read*/
932 ras_stride_reset(ras);
936 ras->ras_stride_pages = ras->ras_consecutive_pages;
937 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
944 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
946 return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
947 ras->ras_stride_pages, ras->ras_stride_offset,
951 /* Stride Read-ahead window will be increased inc_len according to
952 * stride I/O pattern */
953 static void ras_stride_increase_window(struct ll_readahead_state *ras,
954 struct ll_ra_info *ra,
955 unsigned long inc_len)
957 unsigned long left, step, window_len;
958 unsigned long stride_len;
960 LASSERT(ras->ras_stride_length > 0);
961 LASSERTF(ras->ras_window_start + ras->ras_window_len
962 >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
963 " stride_offset %lu\n", ras->ras_window_start,
964 ras->ras_window_len, ras->ras_stride_offset);
966 stride_len = ras->ras_window_start + ras->ras_window_len -
967 ras->ras_stride_offset;
969 left = stride_len % ras->ras_stride_length;
970 window_len = ras->ras_window_len - left;
972 if (left < ras->ras_stride_pages)
975 left = ras->ras_stride_pages + inc_len;
977 LASSERT(ras->ras_stride_pages != 0);
979 step = left / ras->ras_stride_pages;
980 left %= ras->ras_stride_pages;
982 window_len += step * ras->ras_stride_length + left;
984 if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
985 ras->ras_window_len = window_len;
990 static void ras_increase_window(struct inode *inode,
991 struct ll_readahead_state *ras,
992 struct ll_ra_info *ra)
994 /* The stretch of ra-window should be aligned with max rpc_size
995 * but current clio architecture does not support retrieve such
996 * information from lower layer. FIXME later
998 if (stride_io_mode(ras))
999 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP(inode));
1001 ras->ras_window_len = min(ras->ras_window_len +
1002 RAS_INCREASE_STEP(inode),
1003 ra->ra_max_pages_per_file);
1006 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
1007 struct ll_readahead_state *ras, unsigned long index,
1010 struct ll_ra_info *ra = &sbi->ll_ra_info;
1011 int zero = 0, stride_detect = 0, ra_miss = 0;
1014 spin_lock(&ras->ras_lock);
1016 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
1018 /* reset the read-ahead window in two cases. First when the app seeks
1019 * or reads to some other part of the file. Secondly if we get a
1020 * read-ahead miss that we think we've previously issued. This can
1021 * be a symptom of there being so many read-ahead pages that the VM is
1022 * reclaiming it before we get to it. */
1023 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
1025 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
1026 } else if (!hit && ras->ras_window_len &&
1027 index < ras->ras_next_readahead &&
1028 index_in_window(index, ras->ras_window_start, 0,
1029 ras->ras_window_len)) {
1031 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
1034 /* On the second access to a file smaller than the tunable
1035 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
1036 * file up to ra_max_pages_per_file. This is simply a best effort
1037 * and only occurs once per open file. Normal RA behavior is reverted
1038 * to for subsequent IO. The mmap case does not increment
1039 * ras_requests and thus can never trigger this behavior. */
1040 if (ras->ras_requests == 2 && !ras->ras_request_index) {
1043 kms_pages = (i_size_read(inode) + CFS_PAGE_SIZE - 1) >>
1046 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
1047 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
1050 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
1051 ras->ras_window_start = 0;
1052 ras->ras_last_readpage = 0;
1053 ras->ras_next_readahead = 0;
1054 ras->ras_window_len = min(ra->ra_max_pages_per_file,
1055 ra->ra_max_read_ahead_whole_pages);
1056 GOTO(out_unlock, 0);
1060 /* check whether it is in stride I/O mode*/
1061 if (!index_in_stride_window(ras, index)) {
1062 if (ras->ras_consecutive_stride_requests == 0 &&
1063 ras->ras_request_index == 0) {
1064 ras_update_stride_detector(ras, index);
1065 ras->ras_consecutive_stride_requests++;
1067 ras_stride_reset(ras);
1069 ras_reset(inode, ras, index);
1070 ras->ras_consecutive_pages++;
1071 GOTO(out_unlock, 0);
1073 ras->ras_consecutive_pages = 0;
1074 ras->ras_consecutive_requests = 0;
1075 if (++ras->ras_consecutive_stride_requests > 1)
1081 if (index_in_stride_window(ras, index) &&
1082 stride_io_mode(ras)) {
1083 /*If stride-RA hit cache miss, the stride dector
1084 *will not be reset to avoid the overhead of
1085 *redetecting read-ahead mode */
1086 if (index != ras->ras_last_readpage + 1)
1087 ras->ras_consecutive_pages = 0;
1088 ras_reset(inode, ras, index);
1091 /* Reset both stride window and normal RA
1093 ras_reset(inode, ras, index);
1094 ras->ras_consecutive_pages++;
1095 ras_stride_reset(ras);
1096 GOTO(out_unlock, 0);
1098 } else if (stride_io_mode(ras)) {
1099 /* If this is contiguous read but in stride I/O mode
1100 * currently, check whether stride step still is valid,
1101 * if invalid, it will reset the stride ra window*/
1102 if (!index_in_stride_window(ras, index)) {
1103 /* Shrink stride read-ahead window to be zero */
1104 ras_stride_reset(ras);
1105 ras->ras_window_len = 0;
1106 ras->ras_next_readahead = index;
1110 ras->ras_consecutive_pages++;
1111 ras->ras_last_readpage = index;
1112 ras_set_start(inode, ras, index);
1114 if (stride_io_mode(ras))
1115 /* Since stride readahead is sentivite to the offset
1116 * of read-ahead, so we use original offset here,
1117 * instead of ras_window_start, which is RPC aligned */
1118 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
1120 ras->ras_next_readahead = max(ras->ras_window_start,
1121 ras->ras_next_readahead);
1124 /* Trigger RA in the mmap case where ras_consecutive_requests
1125 * is not incremented and thus can't be used to trigger RA */
1126 if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
1127 ras->ras_window_len = RAS_INCREASE_STEP(inode);
1128 GOTO(out_unlock, 0);
1131 /* Initially reset the stride window offset to next_readahead*/
1132 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
1134 * Once stride IO mode is detected, next_readahead should be
1135 * reset to make sure next_readahead > stride offset
1137 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
1138 ras->ras_stride_offset = index;
1139 ras->ras_window_len = RAS_INCREASE_STEP(inode);
1142 /* The initial ras_window_len is set to the request size. To avoid
1143 * uselessly reading and discarding pages for random IO the window is
1144 * only increased once per consecutive request received. */
1145 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
1146 !ras->ras_request_index)
1147 ras_increase_window(inode, ras, ra);
1151 ras->ras_request_index++;
1152 spin_unlock(&ras->ras_lock);
1156 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1158 struct inode *inode = vmpage->mapping->host;
1159 struct ll_inode_info *lli = ll_i2info(inode);
1162 struct cl_page *page;
1163 struct cl_object *clob;
1164 struct cl_env_nest nest;
1165 bool redirtied = false;
1166 bool unlocked = false;
1170 LASSERT(PageLocked(vmpage));
1171 LASSERT(!PageWriteback(vmpage));
1173 LASSERT(ll_i2dtexp(inode) != NULL);
1175 env = cl_env_nested_get(&nest);
1177 GOTO(out, result = PTR_ERR(env));
1179 clob = ll_i2info(inode)->lli_clob;
1180 LASSERT(clob != NULL);
1182 io = ccc_env_thread_io(env);
1184 io->ci_ignore_layout = 1;
1185 result = cl_io_init(env, io, CIT_MISC, clob);
1187 page = cl_page_find(env, clob, vmpage->index,
1188 vmpage, CPT_CACHEABLE);
1189 if (!IS_ERR(page)) {
1190 lu_ref_add(&page->cp_reference, "writepage",
1192 cl_page_assume(env, io, page);
1193 result = cl_page_flush(env, io, page);
1196 * Re-dirty page on error so it retries write,
1197 * but not in case when IO has actually
1198 * occurred and completed with an error.
1200 if (!PageError(vmpage)) {
1201 redirty_page_for_writepage(wbc, vmpage);
1206 cl_page_disown(env, io, page);
1208 lu_ref_del(&page->cp_reference,
1209 "writepage", cfs_current());
1210 cl_page_put(env, page);
1212 result = PTR_ERR(page);
1215 cl_io_fini(env, io);
1217 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1218 loff_t offset = cl_offset(clob, vmpage->index);
1220 /* Flush page failed because the extent is being written out.
1221 * Wait for the write of extent to be finished to avoid
1222 * breaking kernel which assumes ->writepage should mark
1223 * PageWriteback or clean the page. */
1224 result = cl_sync_file_range(inode, offset,
1225 offset + CFS_PAGE_SIZE - 1,
1228 /* actually we may have written more than one page.
1229 * decreasing this page because the caller will count
1231 wbc->nr_to_write -= result - 1;
1236 cl_env_nested_put(&nest, env);
1241 if (!lli->lli_async_rc)
1242 lli->lli_async_rc = result;
1243 SetPageError(vmpage);
1245 unlock_page(vmpage);
1250 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1252 struct inode *inode = mapping->host;
1255 enum cl_fsync_mode mode;
1256 int range_whole = 0;
1260 if (wbc->range_cyclic) {
1261 start = mapping->writeback_index << CFS_PAGE_SHIFT;
1262 end = OBD_OBJECT_EOF;
1264 start = wbc->range_start;
1265 end = wbc->range_end;
1266 if (end == LLONG_MAX) {
1267 end = OBD_OBJECT_EOF;
1268 range_whole = start == 0;
1272 mode = CL_FSYNC_NONE;
1273 if (wbc->sync_mode == WB_SYNC_ALL)
1274 mode = CL_FSYNC_LOCAL;
1276 result = cl_sync_file_range(inode, start, end, mode);
1278 wbc->nr_to_write -= result;
1282 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1283 if (end == OBD_OBJECT_EOF)
1284 end = i_size_read(inode);
1285 mapping->writeback_index = (end >> CFS_PAGE_SHIFT) + 1;
1290 int ll_readpage(struct file *file, struct page *vmpage)
1292 struct ll_cl_context *lcc;
1296 lcc = ll_cl_init(file, vmpage, 0);
1298 struct lu_env *env = lcc->lcc_env;
1299 struct cl_io *io = lcc->lcc_io;
1300 struct cl_page *page = lcc->lcc_page;
1302 LASSERT(page->cp_type == CPT_CACHEABLE);
1303 if (likely(!PageUptodate(vmpage))) {
1304 cl_page_assume(env, io, page);
1305 result = cl_io_read_page(env, io, page);
1307 /* Page from a non-object file. */
1308 LASSERT(!ll_i2info(vmpage->mapping->host)->lli_has_smd);
1309 unlock_page(vmpage);
1314 unlock_page(vmpage);
1315 result = PTR_ERR(lcc);