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/version.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>
66 /* this isn't where truncate starts. roughly:
67 * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate. setattr_raw grabs
68 * DLM lock on [size, EOF], i_mutex, ->lli_size_sem, and WRITE_I_ALLOC_SEM to
71 * must be called under ->lli_size_sem */
72 void ll_truncate(struct inode *inode)
76 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) to %llu\n", inode->i_ino,
77 inode->i_generation, inode, i_size_read(inode));
84 * Finalizes cl-data before exiting typical address_space operation. Dual to
87 static void ll_cl_fini(struct ll_cl_context *lcc)
89 struct lu_env *env = lcc->lcc_env;
90 struct cl_io *io = lcc->lcc_io;
91 struct cl_page *page = lcc->lcc_page;
93 LASSERT(lcc->lcc_cookie == current);
97 lu_ref_del(&page->cp_reference, "cl_io", io);
98 cl_page_put(env, page);
101 if (io && lcc->lcc_created) {
103 cl_io_unlock(env, io);
104 cl_io_iter_fini(env, io);
107 cl_env_put(env, &lcc->lcc_refcheck);
111 * Initializes common cl-data at the typical address_space operation entry
114 static struct ll_cl_context *ll_cl_init(struct file *file,
115 struct page *vmpage, int create)
117 struct ll_cl_context *lcc;
120 struct cl_object *clob;
126 clob = ll_i2info(vmpage->mapping->host)->lli_clob;
127 LASSERT(clob != NULL);
129 env = cl_env_get(&refcheck);
131 return ERR_PTR(PTR_ERR(env));
133 lcc = &vvp_env_info(env)->vti_io_ctx;
134 memset(lcc, 0, sizeof(*lcc));
136 lcc->lcc_refcheck = refcheck;
137 lcc->lcc_cookie = current;
139 cio = ccc_env_io(env);
140 io = cio->cui_cl.cis_io;
141 if (io == NULL && create) {
142 struct inode *inode = vmpage->mapping->host;
145 if (mutex_trylock(&inode->i_mutex)) {
146 mutex_unlock(&(inode)->i_mutex);
148 /* this is too bad. Someone is trying to write the
149 * page w/o holding inode mutex. This means we can
150 * add dirty pages into cache during truncate */
151 CERROR("Proc %s is dirting page w/o inode lock, this"
152 "will break truncate.\n", cfs_current()->comm);
153 libcfs_debug_dumpstack(NULL);
155 return ERR_PTR(-EIO);
159 * Loop-back driver calls ->prepare_write() and ->sendfile()
160 * methods directly, bypassing file system ->write() operation,
161 * so cl_io has to be created here.
163 io = ccc_env_thread_io(env);
164 ll_io_init(io, file, 1);
166 /* No lock at all for this kind of IO - we can't do it because
167 * we have held page lock, it would cause deadlock.
168 * XXX: This causes poor performance to loop device - One page
170 * In order to get better performance, users should use
171 * lloop driver instead.
173 io->ci_lockreq = CILR_NEVER;
175 pos = (vmpage->index << CFS_PAGE_SHIFT);
177 /* Create a temp IO to serve write. */
178 result = cl_io_rw_init(env, io, CIT_WRITE, pos, CFS_PAGE_SIZE);
180 cio->cui_fd = LUSTRE_FPRIVATE(file);
183 result = cl_io_iter_init(env, io);
185 result = cl_io_lock(env, io);
187 result = cl_io_start(env, io);
190 result = io->ci_result;
191 lcc->lcc_created = 1;
198 struct cl_page *page;
201 LASSERT(io->ci_state == CIS_IO_GOING);
202 LASSERT(cio->cui_fd == LUSTRE_FPRIVATE(file));
203 page = cl_page_find(env, clob, vmpage->index, vmpage,
206 lcc->lcc_page = page;
207 lu_ref_add(&page->cp_reference, "cl_io", io);
210 result = PTR_ERR(page);
214 lcc = ERR_PTR(result);
217 CDEBUG(D_VFSTRACE, "%lu@"DFID" -> %d %p %p\n",
218 vmpage->index, PFID(lu_object_fid(&clob->co_lu)), result,
223 static struct ll_cl_context *ll_cl_get(void)
225 struct ll_cl_context *lcc;
229 env = cl_env_get(&refcheck);
230 LASSERT(!IS_ERR(env));
231 lcc = &vvp_env_info(env)->vti_io_ctx;
232 LASSERT(env == lcc->lcc_env);
233 LASSERT(current == lcc->lcc_cookie);
234 cl_env_put(env, &refcheck);
236 /* env has got in ll_cl_init, so it is still usable. */
241 * ->prepare_write() address space operation called by generic_file_write()
242 * for every page during write.
244 int ll_prepare_write(struct file *file, struct page *vmpage, unsigned from,
247 struct ll_cl_context *lcc;
251 lcc = ll_cl_init(file, vmpage, 1);
253 struct lu_env *env = lcc->lcc_env;
254 struct cl_io *io = lcc->lcc_io;
255 struct cl_page *page = lcc->lcc_page;
257 cl_page_assume(env, io, page);
258 if (cl_io_is_append(io)) {
259 struct cl_object *obj = io->ci_obj;
260 struct inode *inode = ccc_object_inode(obj);
262 * In VFS file->page write loop, for appending, the
263 * write offset might be reset according to the new
264 * file size before holding i_mutex. So crw_pos should
265 * be reset here. BUG:17711.
267 io->u.ci_wr.wr.crw_pos = i_size_read(inode);
269 result = cl_io_prepare_write(env, io, page, from, to);
272 * Add a reference, so that page is not evicted from
273 * the cache until ->commit_write() is called.
276 lu_ref_add(&page->cp_reference, "prepare_write",
279 cl_page_unassume(env, io, page);
282 /* returning 0 in prepare assumes commit must be called
285 result = PTR_ERR(lcc);
290 int ll_commit_write(struct file *file, struct page *vmpage, unsigned from,
293 struct ll_cl_context *lcc;
296 struct cl_page *page;
302 page = lcc->lcc_page;
305 LASSERT(cl_page_is_owned(page, io));
307 if (from != to) /* handle short write case. */
308 result = cl_io_commit_write(env, io, page, from, to);
309 if (cl_page_is_owned(page, io))
310 cl_page_unassume(env, io, page);
313 * Release reference acquired by ll_prepare_write().
315 lu_ref_del(&page->cp_reference, "prepare_write", cfs_current());
316 cl_page_put(env, page);
321 struct obd_capa *cl_capa_lookup(struct inode *inode, enum cl_req_type crt)
325 opc = crt == CRT_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
326 return ll_osscapa_get(inode, opc);
329 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
332 * Get readahead pages from the filesystem readahead pool of the client for a
335 * /param sbi superblock for filesystem readahead state ll_ra_info
336 * /param ria per-thread readahead state
337 * /param pages number of pages requested for readahead for the thread.
339 * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
340 * It should work well if the ra_max_pages is much greater than the single
341 * file's read-ahead window, and not too many threads contending for
342 * these readahead pages.
344 * TODO: There may be a 'global sync problem' if many threads are trying
345 * to get an ra budget that is larger than the remaining readahead pages
346 * and reach here at exactly the same time. They will compute /a ret to
347 * consume the remaining pages, but will fail at atomic_add_return() and
348 * get a zero ra window, although there is still ra space remaining. - Jay */
350 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
351 struct ra_io_arg *ria,
354 struct ll_ra_info *ra = &sbi->ll_ra_info;
358 /* If read-ahead pages left are less than 1M, do not do read-ahead,
359 * otherwise it will form small read RPC(< 1M), which hurt server
360 * performance a lot. */
361 ret = min(ra->ra_max_pages - cfs_atomic_read(&ra->ra_cur_pages), pages);
362 if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages))
365 /* If the non-strided (ria_pages == 0) readahead window
366 * (ria_start + ret) has grown across an RPC boundary, then trim
367 * readahead size by the amount beyond the RPC so it ends on an
368 * RPC boundary. If the readahead window is already ending on
369 * an RPC boundary (beyond_rpc == 0), or smaller than a full
370 * RPC (beyond_rpc < ret) the readahead size is unchanged.
371 * The (beyond_rpc != 0) check is skipped since the conditional
372 * branch is more expensive than subtracting zero from the result.
374 * Strided read is left unaligned to avoid small fragments beyond
375 * the RPC boundary from needing an extra read RPC. */
376 if (ria->ria_pages == 0) {
377 long beyond_rpc = (ria->ria_start + ret) % PTLRPC_MAX_BRW_PAGES;
378 if (/* beyond_rpc != 0 && */ beyond_rpc < ret)
382 if (cfs_atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
383 cfs_atomic_sub(ret, &ra->ra_cur_pages);
391 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
393 struct ll_ra_info *ra = &sbi->ll_ra_info;
394 cfs_atomic_sub(len, &ra->ra_cur_pages);
397 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
399 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
400 lprocfs_counter_incr(sbi->ll_ra_stats, which);
403 void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
405 struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
406 ll_ra_stats_inc_sbi(sbi, which);
409 #define RAS_CDEBUG(ras) \
411 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu" \
412 "csr %lu sf %lu sp %lu sl %lu \n", \
413 ras->ras_last_readpage, ras->ras_consecutive_requests, \
414 ras->ras_consecutive_pages, ras->ras_window_start, \
415 ras->ras_window_len, ras->ras_next_readahead, \
416 ras->ras_requests, ras->ras_request_index, \
417 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
418 ras->ras_stride_pages, ras->ras_stride_length)
420 static int index_in_window(unsigned long index, unsigned long point,
421 unsigned long before, unsigned long after)
423 unsigned long start = point - before, end = point + after;
430 return start <= index && index <= end;
433 static struct ll_readahead_state *ll_ras_get(struct file *f)
435 struct ll_file_data *fd;
437 fd = LUSTRE_FPRIVATE(f);
441 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar)
443 struct ll_readahead_state *ras;
447 cfs_spin_lock(&ras->ras_lock);
449 ras->ras_request_index = 0;
450 ras->ras_consecutive_requests++;
451 rar->lrr_reader = current;
453 cfs_list_add(&rar->lrr_linkage, &ras->ras_read_beads);
454 cfs_spin_unlock(&ras->ras_lock);
457 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
459 struct ll_readahead_state *ras;
463 cfs_spin_lock(&ras->ras_lock);
464 cfs_list_del_init(&rar->lrr_linkage);
465 cfs_spin_unlock(&ras->ras_lock);
468 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
470 struct ll_ra_read *scan;
472 cfs_list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
473 if (scan->lrr_reader == current)
479 struct ll_ra_read *ll_ra_read_get(struct file *f)
481 struct ll_readahead_state *ras;
482 struct ll_ra_read *bead;
486 cfs_spin_lock(&ras->ras_lock);
487 bead = ll_ra_read_get_locked(ras);
488 cfs_spin_unlock(&ras->ras_lock);
492 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
493 struct cl_page_list *queue, struct cl_page *page,
502 cl_page_assume(env, io, page);
503 lu_ref_add(&page->cp_reference, "ra", cfs_current());
504 cp = cl2ccc_page(cl_page_at(page, &vvp_device_type));
505 if (!cp->cpg_defer_uptodate && !Page_Uptodate(vmpage)) {
506 rc = cl_page_is_under_lock(env, io, page);
508 cp->cpg_defer_uptodate = 1;
510 cl_page_list_add(queue, page);
513 cl_page_delete(env, page);
517 /* skip completed pages */
518 cl_page_unassume(env, io, page);
519 lu_ref_del(&page->cp_reference, "ra", cfs_current());
520 cl_page_put(env, page);
525 * Initiates read-ahead of a page with given index.
527 * \retval +ve: page was added to \a queue.
529 * \retval -ENOLCK: there is no extent lock for this part of a file, stop
532 * \retval -ve, 0: page wasn't added to \a queue for other reason.
534 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
535 struct cl_page_list *queue,
536 pgoff_t index, struct address_space *mapping)
539 struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
540 struct cl_page *page;
541 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
542 unsigned int gfp_mask;
544 const char *msg = NULL;
548 gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
550 gfp_mask |= __GFP_NOWARN;
552 vmpage = grab_cache_page_nowait(mapping, index);
553 if (vmpage != NULL) {
554 /* Check if vmpage was truncated or reclaimed */
555 if (vmpage->mapping == mapping) {
556 page = cl_page_find(env, clob, vmpage->index,
557 vmpage, CPT_CACHEABLE);
559 rc = cl_read_ahead_page(env, io, queue,
562 which = RA_STAT_FAILED_MATCH;
563 msg = "lock match failed";
566 which = RA_STAT_FAILED_GRAB_PAGE;
567 msg = "cl_page_find failed";
570 which = RA_STAT_WRONG_GRAB_PAGE;
571 msg = "g_c_p_n returned invalid page";
575 page_cache_release(vmpage);
577 which = RA_STAT_FAILED_GRAB_PAGE;
578 msg = "g_c_p_n failed";
581 ll_ra_stats_inc(mapping, which);
582 CDEBUG(D_READA, "%s\n", msg);
587 #define RIA_DEBUG(ria) \
588 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
589 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
592 #define RAS_INCREASE_STEP PTLRPC_MAX_BRW_PAGES
594 static inline int stride_io_mode(struct ll_readahead_state *ras)
596 return ras->ras_consecutive_stride_requests > 1;
598 /* The function calculates how much pages will be read in
599 * [off, off + length], in such stride IO area,
600 * stride_offset = st_off, stride_lengh = st_len,
601 * stride_pages = st_pgs
603 * |------------------|*****|------------------|*****|------------|*****|....
606 * |----- st_len -----|
608 * How many pages it should read in such pattern
609 * |-------------------------------------------------------------|
611 * |<------ length ------->|
613 * = |<----->| + |-------------------------------------| + |---|
614 * start_left st_pgs * i end_left
617 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
618 unsigned long off, unsigned long length)
620 __u64 start = off > st_off ? off - st_off : 0;
621 __u64 end = off + length > st_off ? off + length - st_off : 0;
622 unsigned long start_left = 0;
623 unsigned long end_left = 0;
624 unsigned long pg_count;
626 if (st_len == 0 || length == 0 || end == 0)
629 start_left = do_div(start, st_len);
630 if (start_left < st_pgs)
631 start_left = st_pgs - start_left;
635 end_left = do_div(end, st_len);
636 if (end_left > st_pgs)
639 CDEBUG(D_READA, "start "LPU64", end "LPU64" start_left %lu end_left %lu \n",
640 start, end, start_left, end_left);
643 pg_count = end_left - (st_pgs - start_left);
645 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
647 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
648 "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
653 static int ria_page_count(struct ra_io_arg *ria)
655 __u64 length = ria->ria_end >= ria->ria_start ?
656 ria->ria_end - ria->ria_start + 1 : 0;
658 return stride_pg_count(ria->ria_stoff, ria->ria_length,
659 ria->ria_pages, ria->ria_start,
663 /*Check whether the index is in the defined ra-window */
664 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
666 /* If ria_length == ria_pages, it means non-stride I/O mode,
667 * idx should always inside read-ahead window in this case
668 * For stride I/O mode, just check whether the idx is inside
670 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
671 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
672 ria->ria_length < ria->ria_pages);
675 static int ll_read_ahead_pages(const struct lu_env *env,
676 struct cl_io *io, struct cl_page_list *queue,
677 struct ra_io_arg *ria,
678 unsigned long *reserved_pages,
679 struct address_space *mapping,
680 unsigned long *ra_end)
682 int rc, count = 0, stride_ria;
683 unsigned long page_idx;
685 LASSERT(ria != NULL);
688 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
689 for (page_idx = ria->ria_start; page_idx <= ria->ria_end &&
690 *reserved_pages > 0; page_idx++) {
691 if (ras_inside_ra_window(page_idx, ria)) {
692 /* If the page is inside the read-ahead window*/
693 rc = ll_read_ahead_page(env, io, queue,
698 } else if (rc == -ENOLCK)
700 } else if (stride_ria) {
701 /* If it is not in the read-ahead window, and it is
702 * read-ahead mode, then check whether it should skip
705 /* FIXME: This assertion only is valid when it is for
706 * forward read-ahead, it will be fixed when backward
707 * read-ahead is implemented */
708 LASSERTF(page_idx > ria->ria_stoff, "Invalid page_idx %lu"
709 "rs %lu re %lu ro %lu rl %lu rp %lu\n", page_idx,
710 ria->ria_start, ria->ria_end, ria->ria_stoff,
711 ria->ria_length, ria->ria_pages);
712 offset = page_idx - ria->ria_stoff;
713 offset = offset % (ria->ria_length);
714 if (offset > ria->ria_pages) {
715 page_idx += ria->ria_length - offset;
716 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
717 ria->ria_length - offset);
726 int ll_readahead(const struct lu_env *env, struct cl_io *io,
727 struct ll_readahead_state *ras, struct address_space *mapping,
728 struct cl_page_list *queue, int flags)
730 struct vvp_io *vio = vvp_env_io(env);
731 struct vvp_thread_info *vti = vvp_env_info(env);
732 struct cl_attr *attr = ccc_env_thread_attr(env);
733 unsigned long start = 0, end = 0, reserved;
734 unsigned long ra_end, len;
736 struct ll_ra_read *bead;
737 struct ra_io_arg *ria = &vti->vti_ria;
738 struct ll_inode_info *lli;
739 struct cl_object *clob;
744 inode = mapping->host;
745 lli = ll_i2info(inode);
746 clob = lli->lli_clob;
748 memset(ria, 0, sizeof *ria);
750 cl_object_attr_lock(clob);
751 ret = cl_object_attr_get(env, clob, attr);
752 cl_object_attr_unlock(clob);
758 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
762 cfs_spin_lock(&ras->ras_lock);
763 if (vio->cui_ra_window_set)
764 bead = &vio->cui_bead;
768 /* Enlarge the RA window to encompass the full read */
769 if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
770 bead->lrr_start + bead->lrr_count) {
771 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
772 ras->ras_window_start;
774 /* Reserve a part of the read-ahead window that we'll be issuing */
775 if (ras->ras_window_len) {
776 start = ras->ras_next_readahead;
777 end = ras->ras_window_start + ras->ras_window_len - 1;
780 unsigned long rpc_boundary;
782 * Align RA window to an optimal boundary.
784 * XXX This would be better to align to cl_max_pages_per_rpc
785 * instead of PTLRPC_MAX_BRW_PAGES, because the RPC size may
786 * be aligned to the RAID stripe size in the future and that
787 * is more important than the RPC size.
789 /* Note: we only trim the RPC, instead of extending the RPC
790 * to the boundary, so to avoid reading too much pages during
792 rpc_boundary = ((end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1)));
793 if (rpc_boundary > 0)
796 if (rpc_boundary > start)
799 /* Truncate RA window to end of file */
800 end = min(end, (unsigned long)((kms - 1) >> CFS_PAGE_SHIFT));
802 ras->ras_next_readahead = max(end, end + 1);
805 ria->ria_start = start;
807 /* If stride I/O mode is detected, get stride window*/
808 if (stride_io_mode(ras)) {
809 ria->ria_stoff = ras->ras_stride_offset;
810 ria->ria_length = ras->ras_stride_length;
811 ria->ria_pages = ras->ras_stride_pages;
813 cfs_spin_unlock(&ras->ras_lock);
816 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
819 len = ria_page_count(ria);
823 reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len);
825 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
827 CDEBUG(D_READA, "reserved page %lu ra_cur %d ra_max %lu\n", reserved,
828 cfs_atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
829 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
831 ret = ll_read_ahead_pages(env, io, queue,
832 ria, &reserved, mapping, &ra_end);
834 LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
836 ll_ra_count_put(ll_i2sbi(inode), reserved);
838 if (ra_end == end + 1 && ra_end == (kms >> CFS_PAGE_SHIFT))
839 ll_ra_stats_inc(mapping, RA_STAT_EOF);
841 /* if we didn't get to the end of the region we reserved from
842 * the ras we need to go back and update the ras so that the
843 * next read-ahead tries from where we left off. we only do so
844 * if the region we failed to issue read-ahead on is still ahead
845 * of the app and behind the next index to start read-ahead from */
846 CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
847 ra_end, end, ria->ria_end);
849 if (ra_end != end + 1) {
850 cfs_spin_lock(&ras->ras_lock);
851 if (ra_end < ras->ras_next_readahead &&
852 index_in_window(ra_end, ras->ras_window_start, 0,
853 ras->ras_window_len)) {
854 ras->ras_next_readahead = ra_end;
857 cfs_spin_unlock(&ras->ras_lock);
863 static void ras_set_start(struct ll_readahead_state *ras, unsigned long index)
865 ras->ras_window_start = index & (~(RAS_INCREASE_STEP - 1));
868 /* called with the ras_lock held or from places where it doesn't matter */
869 static void ras_reset(struct ll_readahead_state *ras, unsigned long index)
871 ras->ras_last_readpage = index;
872 ras->ras_consecutive_requests = 0;
873 ras->ras_consecutive_pages = 0;
874 ras->ras_window_len = 0;
875 ras_set_start(ras, index);
876 ras->ras_next_readahead = max(ras->ras_window_start, index);
881 /* called with the ras_lock held or from places where it doesn't matter */
882 static void ras_stride_reset(struct ll_readahead_state *ras)
884 ras->ras_consecutive_stride_requests = 0;
885 ras->ras_stride_length = 0;
886 ras->ras_stride_pages = 0;
890 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
892 cfs_spin_lock_init(&ras->ras_lock);
894 ras->ras_requests = 0;
895 CFS_INIT_LIST_HEAD(&ras->ras_read_beads);
899 * Check whether the read request is in the stride window.
900 * If it is in the stride window, return 1, otherwise return 0.
902 static int index_in_stride_window(unsigned long index,
903 struct ll_readahead_state *ras,
906 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
908 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
909 ras->ras_stride_pages == ras->ras_stride_length)
912 /* If it is contiguous read */
914 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
916 /*Otherwise check the stride by itself */
917 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
918 ras->ras_consecutive_pages == ras->ras_stride_pages;
921 static void ras_update_stride_detector(struct ll_readahead_state *ras,
924 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
926 if (!stride_io_mode(ras) && (stride_gap != 0 ||
927 ras->ras_consecutive_stride_requests == 0)) {
928 ras->ras_stride_pages = ras->ras_consecutive_pages;
929 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
931 LASSERT(ras->ras_request_index == 0);
932 LASSERT(ras->ras_consecutive_stride_requests == 0);
934 if (index <= ras->ras_last_readpage) {
935 /*Reset stride window for forward read*/
936 ras_stride_reset(ras);
940 ras->ras_stride_pages = ras->ras_consecutive_pages;
941 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
948 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
950 return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
951 ras->ras_stride_pages, ras->ras_stride_offset,
955 /* Stride Read-ahead window will be increased inc_len according to
956 * stride I/O pattern */
957 static void ras_stride_increase_window(struct ll_readahead_state *ras,
958 struct ll_ra_info *ra,
959 unsigned long inc_len)
961 unsigned long left, step, window_len;
962 unsigned long stride_len;
964 LASSERT(ras->ras_stride_length > 0);
965 LASSERTF(ras->ras_window_start + ras->ras_window_len
966 >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
967 " stride_offset %lu\n", ras->ras_window_start,
968 ras->ras_window_len, ras->ras_stride_offset);
970 stride_len = ras->ras_window_start + ras->ras_window_len -
971 ras->ras_stride_offset;
973 left = stride_len % ras->ras_stride_length;
974 window_len = ras->ras_window_len - left;
976 if (left < ras->ras_stride_pages)
979 left = ras->ras_stride_pages + inc_len;
981 LASSERT(ras->ras_stride_pages != 0);
983 step = left / ras->ras_stride_pages;
984 left %= ras->ras_stride_pages;
986 window_len += step * ras->ras_stride_length + left;
988 if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
989 ras->ras_window_len = window_len;
994 static void ras_increase_window(struct ll_readahead_state *ras,
995 struct ll_ra_info *ra, struct inode *inode)
997 /* The stretch of ra-window should be aligned with max rpc_size
998 * but current clio architecture does not support retrieve such
999 * information from lower layer. FIXME later
1001 if (stride_io_mode(ras))
1002 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP);
1004 ras->ras_window_len = min(ras->ras_window_len +
1006 ra->ra_max_pages_per_file);
1009 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
1010 struct ll_readahead_state *ras, unsigned long index,
1013 struct ll_ra_info *ra = &sbi->ll_ra_info;
1014 int zero = 0, stride_detect = 0, ra_miss = 0;
1017 cfs_spin_lock(&ras->ras_lock);
1019 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
1021 /* reset the read-ahead window in two cases. First when the app seeks
1022 * or reads to some other part of the file. Secondly if we get a
1023 * read-ahead miss that we think we've previously issued. This can
1024 * be a symptom of there being so many read-ahead pages that the VM is
1025 * reclaiming it before we get to it. */
1026 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
1028 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
1029 } else if (!hit && ras->ras_window_len &&
1030 index < ras->ras_next_readahead &&
1031 index_in_window(index, ras->ras_window_start, 0,
1032 ras->ras_window_len)) {
1034 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
1037 /* On the second access to a file smaller than the tunable
1038 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
1039 * file up to ra_max_pages_per_file. This is simply a best effort
1040 * and only occurs once per open file. Normal RA behavior is reverted
1041 * to for subsequent IO. The mmap case does not increment
1042 * ras_requests and thus can never trigger this behavior. */
1043 if (ras->ras_requests == 2 && !ras->ras_request_index) {
1046 kms_pages = (i_size_read(inode) + CFS_PAGE_SIZE - 1) >>
1049 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
1050 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
1053 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
1054 ras->ras_window_start = 0;
1055 ras->ras_last_readpage = 0;
1056 ras->ras_next_readahead = 0;
1057 ras->ras_window_len = min(ra->ra_max_pages_per_file,
1058 ra->ra_max_read_ahead_whole_pages);
1059 GOTO(out_unlock, 0);
1063 /* check whether it is in stride I/O mode*/
1064 if (!index_in_stride_window(index, ras, inode)) {
1065 if (ras->ras_consecutive_stride_requests == 0 &&
1066 ras->ras_request_index == 0) {
1067 ras_update_stride_detector(ras, index);
1068 ras->ras_consecutive_stride_requests ++;
1070 ras_stride_reset(ras);
1072 ras_reset(ras, index);
1073 ras->ras_consecutive_pages++;
1074 GOTO(out_unlock, 0);
1076 ras->ras_consecutive_pages = 0;
1077 ras->ras_consecutive_requests = 0;
1078 if (++ras->ras_consecutive_stride_requests > 1)
1084 if (index_in_stride_window(index, ras, inode) &&
1085 stride_io_mode(ras)) {
1086 /*If stride-RA hit cache miss, the stride dector
1087 *will not be reset to avoid the overhead of
1088 *redetecting read-ahead mode */
1089 if (index != ras->ras_last_readpage + 1)
1090 ras->ras_consecutive_pages = 0;
1091 ras_reset(ras, index);
1094 /* Reset both stride window and normal RA
1096 ras_reset(ras, index);
1097 ras->ras_consecutive_pages++;
1098 ras_stride_reset(ras);
1099 GOTO(out_unlock, 0);
1101 } else if (stride_io_mode(ras)) {
1102 /* If this is contiguous read but in stride I/O mode
1103 * currently, check whether stride step still is valid,
1104 * if invalid, it will reset the stride ra window*/
1105 if (!index_in_stride_window(index, ras, inode)) {
1106 /* Shrink stride read-ahead window to be zero */
1107 ras_stride_reset(ras);
1108 ras->ras_window_len = 0;
1109 ras->ras_next_readahead = index;
1113 ras->ras_consecutive_pages++;
1114 ras->ras_last_readpage = index;
1115 ras_set_start(ras, index);
1117 if (stride_io_mode(ras))
1118 /* Since stride readahead is sentivite to the offset
1119 * of read-ahead, so we use original offset here,
1120 * instead of ras_window_start, which is 1M aligned*/
1121 ras->ras_next_readahead = max(index,
1122 ras->ras_next_readahead);
1124 ras->ras_next_readahead = max(ras->ras_window_start,
1125 ras->ras_next_readahead);
1128 /* Trigger RA in the mmap case where ras_consecutive_requests
1129 * is not incremented and thus can't be used to trigger RA */
1130 if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
1131 ras->ras_window_len = RAS_INCREASE_STEP;
1132 GOTO(out_unlock, 0);
1135 /* Initially reset the stride window offset to next_readahead*/
1136 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
1138 * Once stride IO mode is detected, next_readahead should be
1139 * reset to make sure next_readahead > stride offset
1141 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
1142 ras->ras_stride_offset = index;
1143 ras->ras_window_len = RAS_INCREASE_STEP;
1146 /* The initial ras_window_len is set to the request size. To avoid
1147 * uselessly reading and discarding pages for random IO the window is
1148 * only increased once per consecutive request received. */
1149 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
1150 !ras->ras_request_index)
1151 ras_increase_window(ras, ra, inode);
1155 ras->ras_request_index++;
1156 cfs_spin_unlock(&ras->ras_lock);
1160 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1162 struct inode *inode = vmpage->mapping->host;
1163 struct ll_inode_info *lli = ll_i2info(inode);
1166 struct cl_page *page;
1167 struct cl_object *clob;
1168 struct cl_env_nest nest;
1169 bool redirtied = false;
1170 bool unlocked = false;
1174 LASSERT(PageLocked(vmpage));
1175 LASSERT(!PageWriteback(vmpage));
1177 LASSERT(ll_i2dtexp(inode) != NULL);
1179 env = cl_env_nested_get(&nest);
1181 GOTO(out, result = PTR_ERR(env));
1183 clob = ll_i2info(inode)->lli_clob;
1184 LASSERT(clob != NULL);
1186 io = ccc_env_thread_io(env);
1188 io->ci_ignore_layout = 1;
1189 result = cl_io_init(env, io, CIT_MISC, clob);
1191 page = cl_page_find(env, clob, vmpage->index,
1192 vmpage, CPT_CACHEABLE);
1193 if (!IS_ERR(page)) {
1194 lu_ref_add(&page->cp_reference, "writepage",
1196 cl_page_assume(env, io, page);
1197 result = cl_page_flush(env, io, page);
1200 * Re-dirty page on error so it retries write,
1201 * but not in case when IO has actually
1202 * occurred and completed with an error.
1204 if (!PageError(vmpage)) {
1205 redirty_page_for_writepage(wbc, vmpage);
1210 cl_page_disown(env, io, page);
1212 lu_ref_del(&page->cp_reference,
1213 "writepage", cfs_current());
1214 cl_page_put(env, page);
1216 result = PTR_ERR(page);
1219 cl_io_fini(env, io);
1221 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1222 loff_t offset = cl_offset(clob, vmpage->index);
1224 /* Flush page failed because the extent is being written out.
1225 * Wait for the write of extent to be finished to avoid
1226 * breaking kernel which assumes ->writepage should mark
1227 * PageWriteback or clean the page. */
1228 result = cl_sync_file_range(inode, offset,
1229 offset + CFS_PAGE_SIZE - 1,
1232 /* actually we may have written more than one page.
1233 * decreasing this page because the caller will count
1235 wbc->nr_to_write -= result - 1;
1240 cl_env_nested_put(&nest, env);
1245 if (!lli->lli_async_rc)
1246 lli->lli_async_rc = result;
1247 SetPageError(vmpage);
1249 unlock_page(vmpage);
1254 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1256 struct inode *inode = mapping->host;
1259 enum cl_fsync_mode mode;
1260 int range_whole = 0;
1264 if (wbc->range_cyclic) {
1265 start = mapping->writeback_index << CFS_PAGE_SHIFT;
1266 end = OBD_OBJECT_EOF;
1268 start = wbc->range_start;
1269 end = wbc->range_end;
1270 if (end == LLONG_MAX) {
1271 end = OBD_OBJECT_EOF;
1272 range_whole = start == 0;
1276 mode = CL_FSYNC_NONE;
1277 if (wbc->sync_mode == WB_SYNC_ALL)
1278 mode = CL_FSYNC_LOCAL;
1280 result = cl_sync_file_range(inode, start, end, mode);
1282 wbc->nr_to_write -= result;
1286 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1287 if (end == OBD_OBJECT_EOF)
1288 end = i_size_read(inode);
1289 mapping->writeback_index = (end >> CFS_PAGE_SHIFT) + 1;
1294 int ll_readpage(struct file *file, struct page *vmpage)
1296 struct ll_cl_context *lcc;
1300 lcc = ll_cl_init(file, vmpage, 0);
1302 struct lu_env *env = lcc->lcc_env;
1303 struct cl_io *io = lcc->lcc_io;
1304 struct cl_page *page = lcc->lcc_page;
1306 LASSERT(page->cp_type == CPT_CACHEABLE);
1307 if (likely(!PageUptodate(vmpage))) {
1308 cl_page_assume(env, io, page);
1309 result = cl_io_read_page(env, io, page);
1311 /* Page from a non-object file. */
1312 LASSERT(!ll_i2info(vmpage->mapping->host)->lli_has_smd);
1313 unlock_page(vmpage);
1318 unlock_page(vmpage);
1319 result = PTR_ERR(lcc);