blob: 336397773fbb5d911c54081624405818237f13fe [file] [log] [blame]
Peng Taod7e09d02013-05-02 16:46:55 +08001/*
2 * GPL HEADER START
3 *
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
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.
9 *
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).
15 *
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
19 *
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
22 * have any questions.
23 *
24 * GPL HEADER END
25 */
26/*
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
29 *
Andreas Dilger1dc563a2015-11-08 18:09:37 -050030 * Copyright (c) 2011, 2015, Intel Corporation.
Peng Taod7e09d02013-05-02 16:46:55 +080031 */
32/*
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
35 *
36 * lustre/llite/rw.c
37 *
38 * Lustre Lite I/O page cache routines shared by different kernel revs
39 */
40
41#include <linux/kernel.h>
42#include <linux/mm.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>
Asaf Vertze8fd99f2015-02-09 12:00:50 +020048#include <linux/uaccess.h>
Peng Taod7e09d02013-05-02 16:46:55 +080049
50#include <linux/fs.h>
Peng Taod7e09d02013-05-02 16:46:55 +080051#include <linux/pagemap.h>
52/* current_is_kswapd() */
53#include <linux/swap.h>
54
55#define DEBUG_SUBSYSTEM S_LLITE
56
Greg Kroah-Hartman67a235f2014-07-11 21:51:41 -070057#include "../include/lustre_lite.h"
58#include "../include/obd_cksum.h"
Peng Taod7e09d02013-05-02 16:46:55 +080059#include "llite_internal.h"
Greg Kroah-Hartman67a235f2014-07-11 21:51:41 -070060#include "../include/linux/lustre_compat25.h"
Peng Taod7e09d02013-05-02 16:46:55 +080061
62/**
63 * Finalizes cl-data before exiting typical address_space operation. Dual to
64 * ll_cl_init().
65 */
Jinshan Xiong77605e42016-03-30 19:48:30 -040066void ll_cl_fini(struct ll_cl_context *lcc)
Peng Taod7e09d02013-05-02 16:46:55 +080067{
68 struct lu_env *env = lcc->lcc_env;
69 struct cl_io *io = lcc->lcc_io;
70 struct cl_page *page = lcc->lcc_page;
71
72 LASSERT(lcc->lcc_cookie == current);
Oleg Drokin6e168182016-02-16 00:46:46 -050073 LASSERT(env);
Peng Taod7e09d02013-05-02 16:46:55 +080074
Oleg Drokin6e168182016-02-16 00:46:46 -050075 if (page) {
Peng Taod7e09d02013-05-02 16:46:55 +080076 lu_ref_del(&page->cp_reference, "cl_io", io);
77 cl_page_put(env, page);
78 }
79
Peng Taod7e09d02013-05-02 16:46:55 +080080 cl_env_put(env, &lcc->lcc_refcheck);
81}
82
83/**
84 * Initializes common cl-data at the typical address_space operation entry
85 * point.
86 */
Jinshan Xiong77605e42016-03-30 19:48:30 -040087struct ll_cl_context *ll_cl_init(struct file *file, struct page *vmpage)
Peng Taod7e09d02013-05-02 16:46:55 +080088{
89 struct ll_cl_context *lcc;
90 struct lu_env *env;
91 struct cl_io *io;
92 struct cl_object *clob;
John L. Hammonde0a81442016-03-30 19:48:52 -040093 struct vvp_io *vio;
Peng Taod7e09d02013-05-02 16:46:55 +080094
95 int refcheck;
96 int result = 0;
97
Jinshan Xiong77605e42016-03-30 19:48:30 -040098 clob = ll_i2info(file_inode(file))->lli_clob;
Oleg Drokin6e168182016-02-16 00:46:46 -050099 LASSERT(clob);
Peng Taod7e09d02013-05-02 16:46:55 +0800100
101 env = cl_env_get(&refcheck);
102 if (IS_ERR(env))
Laurent Navet1dc85482013-07-05 14:26:33 +0200103 return ERR_CAST(env);
Peng Taod7e09d02013-05-02 16:46:55 +0800104
John Hammond9989a582016-03-30 19:48:56 -0400105 lcc = &ll_env_info(env)->lti_io_ctx;
Peng Taod7e09d02013-05-02 16:46:55 +0800106 memset(lcc, 0, sizeof(*lcc));
107 lcc->lcc_env = env;
108 lcc->lcc_refcheck = refcheck;
109 lcc->lcc_cookie = current;
110
John L. Hammonde0a81442016-03-30 19:48:52 -0400111 vio = vvp_env_io(env);
112 io = vio->vui_cl.cis_io;
Peng Taod7e09d02013-05-02 16:46:55 +0800113 lcc->lcc_io = io;
Bobi Jam06d2fcc2016-04-12 21:11:09 -0400114 if (!io)
Peng Taod7e09d02013-05-02 16:46:55 +0800115 result = -EIO;
Bobi Jam06d2fcc2016-04-12 21:11:09 -0400116
Jinshan Xiong77605e42016-03-30 19:48:30 -0400117 if (result == 0 && vmpage) {
Peng Taod7e09d02013-05-02 16:46:55 +0800118 struct cl_page *page;
119
Peng Taod7e09d02013-05-02 16:46:55 +0800120 LASSERT(io->ci_state == CIS_IO_GOING);
John L. Hammonde0a81442016-03-30 19:48:52 -0400121 LASSERT(vio->vui_fd == LUSTRE_FPRIVATE(file));
Peng Taod7e09d02013-05-02 16:46:55 +0800122 page = cl_page_find(env, clob, vmpage->index, vmpage,
123 CPT_CACHEABLE);
124 if (!IS_ERR(page)) {
125 lcc->lcc_page = page;
126 lu_ref_add(&page->cp_reference, "cl_io", io);
127 result = 0;
Oleg Drokinda5ecb42016-04-01 15:18:01 -0400128 } else {
Peng Taod7e09d02013-05-02 16:46:55 +0800129 result = PTR_ERR(page);
Oleg Drokinda5ecb42016-04-01 15:18:01 -0400130 }
Peng Taod7e09d02013-05-02 16:46:55 +0800131 }
132 if (result) {
133 ll_cl_fini(lcc);
134 lcc = ERR_PTR(result);
135 }
136
Peng Taod7e09d02013-05-02 16:46:55 +0800137 return lcc;
138}
139
Peng Taod7e09d02013-05-02 16:46:55 +0800140static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
141
142/**
143 * Get readahead pages from the filesystem readahead pool of the client for a
144 * thread.
145 *
146 * /param sbi superblock for filesystem readahead state ll_ra_info
147 * /param ria per-thread readahead state
148 * /param pages number of pages requested for readahead for the thread.
149 *
150 * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
151 * It should work well if the ra_max_pages is much greater than the single
152 * file's read-ahead window, and not too many threads contending for
153 * these readahead pages.
154 *
155 * TODO: There may be a 'global sync problem' if many threads are trying
156 * to get an ra budget that is larger than the remaining readahead pages
157 * and reach here at exactly the same time. They will compute /a ret to
158 * consume the remaining pages, but will fail at atomic_add_return() and
Oleg Drokinc0894c62016-02-24 22:00:30 -0500159 * get a zero ra window, although there is still ra space remaining. - Jay
160 */
Peng Taod7e09d02013-05-02 16:46:55 +0800161static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
162 struct ra_io_arg *ria,
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400163 unsigned long pages, unsigned long min)
Peng Taod7e09d02013-05-02 16:46:55 +0800164{
165 struct ll_ra_info *ra = &sbi->ll_ra_info;
166 long ret;
Peng Taod7e09d02013-05-02 16:46:55 +0800167
168 /* If read-ahead pages left are less than 1M, do not do read-ahead,
169 * otherwise it will form small read RPC(< 1M), which hurt server
Oleg Drokinc0894c62016-02-24 22:00:30 -0500170 * performance a lot.
171 */
Peng Taod7e09d02013-05-02 16:46:55 +0800172 ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages), pages);
Julia Lawall34e1f2b2014-08-30 16:24:55 +0200173 if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages)) {
174 ret = 0;
175 goto out;
176 }
Peng Taod7e09d02013-05-02 16:46:55 +0800177
178 /* If the non-strided (ria_pages == 0) readahead window
179 * (ria_start + ret) has grown across an RPC boundary, then trim
180 * readahead size by the amount beyond the RPC so it ends on an
181 * RPC boundary. If the readahead window is already ending on
182 * an RPC boundary (beyond_rpc == 0), or smaller than a full
183 * RPC (beyond_rpc < ret) the readahead size is unchanged.
184 * The (beyond_rpc != 0) check is skipped since the conditional
185 * branch is more expensive than subtracting zero from the result.
186 *
187 * Strided read is left unaligned to avoid small fragments beyond
Oleg Drokinc0894c62016-02-24 22:00:30 -0500188 * the RPC boundary from needing an extra read RPC.
189 */
Peng Taod7e09d02013-05-02 16:46:55 +0800190 if (ria->ria_pages == 0) {
191 long beyond_rpc = (ria->ria_start + ret) % PTLRPC_MAX_BRW_PAGES;
Mike Rapoport50ffcb72015-10-13 16:03:40 +0300192
Peng Taod7e09d02013-05-02 16:46:55 +0800193 if (/* beyond_rpc != 0 && */ beyond_rpc < ret)
194 ret -= beyond_rpc;
195 }
196
197 if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
198 atomic_sub(ret, &ra->ra_cur_pages);
199 ret = 0;
200 }
201
202out:
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400203 if (ret < min) {
204 /* override ra limit for maximum performance */
205 atomic_add(min - ret, &ra->ra_cur_pages);
206 ret = min;
207 }
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800208 return ret;
Peng Taod7e09d02013-05-02 16:46:55 +0800209}
210
211void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
212{
213 struct ll_ra_info *ra = &sbi->ll_ra_info;
Mike Rapoport50ffcb72015-10-13 16:03:40 +0300214
Peng Taod7e09d02013-05-02 16:46:55 +0800215 atomic_sub(len, &ra->ra_cur_pages);
216}
217
218static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
219{
220 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
221 lprocfs_counter_incr(sbi->ll_ra_stats, which);
222}
223
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400224void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
Peng Taod7e09d02013-05-02 16:46:55 +0800225{
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400226 struct ll_sb_info *sbi = ll_i2sbi(inode);
Mike Rapoport50ffcb72015-10-13 16:03:40 +0300227
Peng Taod7e09d02013-05-02 16:46:55 +0800228 ll_ra_stats_inc_sbi(sbi, which);
229}
230
231#define RAS_CDEBUG(ras) \
232 CDEBUG(D_READA, \
233 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu" \
Oleg Drokin72a87fc2016-02-24 22:00:40 -0500234 "csr %lu sf %lu sp %lu sl %lu\n", \
Peng Taod7e09d02013-05-02 16:46:55 +0800235 ras->ras_last_readpage, ras->ras_consecutive_requests, \
236 ras->ras_consecutive_pages, ras->ras_window_start, \
237 ras->ras_window_len, ras->ras_next_readahead, \
238 ras->ras_requests, ras->ras_request_index, \
239 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
240 ras->ras_stride_pages, ras->ras_stride_length)
241
242static int index_in_window(unsigned long index, unsigned long point,
243 unsigned long before, unsigned long after)
244{
245 unsigned long start = point - before, end = point + after;
246
247 if (start > point)
Oleg Drokindefa2202016-02-24 22:00:39 -0500248 start = 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800249 if (end < point)
Oleg Drokindefa2202016-02-24 22:00:39 -0500250 end = ~0;
Peng Taod7e09d02013-05-02 16:46:55 +0800251
252 return start <= index && index <= end;
253}
254
John L. Hammondbc4320a2016-03-30 19:48:50 -0400255void ll_ras_enter(struct file *f)
Peng Taod7e09d02013-05-02 16:46:55 +0800256{
John L. Hammondbc4320a2016-03-30 19:48:50 -0400257 struct ll_file_data *fd = LUSTRE_FPRIVATE(f);
258 struct ll_readahead_state *ras = &fd->fd_ras;
Peng Taod7e09d02013-05-02 16:46:55 +0800259
260 spin_lock(&ras->ras_lock);
261 ras->ras_requests++;
262 ras->ras_request_index = 0;
263 ras->ras_consecutive_requests++;
Peng Taod7e09d02013-05-02 16:46:55 +0800264 spin_unlock(&ras->ras_lock);
265}
266
Peng Taod7e09d02013-05-02 16:46:55 +0800267static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
268 struct cl_page_list *queue, struct cl_page *page,
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400269 struct cl_object *clob, pgoff_t *max_index)
Peng Taod7e09d02013-05-02 16:46:55 +0800270{
Jinshan Xiong7addf402016-03-30 19:48:32 -0400271 struct page *vmpage = page->cp_vmpage;
John L. Hammond3a52f802016-03-30 19:48:48 -0400272 struct vvp_page *vpg;
Peng Taod7e09d02013-05-02 16:46:55 +0800273 int rc;
274
Peng Taod7e09d02013-05-02 16:46:55 +0800275 rc = 0;
276 cl_page_assume(env, io, page);
277 lu_ref_add(&page->cp_reference, "ra", current);
John L. Hammond3a52f802016-03-30 19:48:48 -0400278 vpg = cl2vvp_page(cl_object_page_slice(clob, page));
279 if (!vpg->vpg_defer_uptodate && !PageUptodate(vmpage)) {
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400280 CDEBUG(D_READA, "page index %lu, max_index: %lu\n",
John L. Hammond3a52f802016-03-30 19:48:48 -0400281 vvp_index(vpg), *max_index);
282 if (*max_index == 0 || vvp_index(vpg) > *max_index)
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400283 rc = cl_page_is_under_lock(env, io, page, max_index);
284 if (rc == 0) {
John L. Hammond3a52f802016-03-30 19:48:48 -0400285 vpg->vpg_defer_uptodate = 1;
286 vpg->vpg_ra_used = 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800287 cl_page_list_add(queue, page);
288 rc = 1;
289 } else {
Jinshan Xiongd9d47902016-03-30 19:48:28 -0400290 cl_page_discard(env, io, page);
Peng Taod7e09d02013-05-02 16:46:55 +0800291 rc = -ENOLCK;
292 }
293 } else {
294 /* skip completed pages */
295 cl_page_unassume(env, io, page);
296 }
297 lu_ref_del(&page->cp_reference, "ra", current);
298 cl_page_put(env, page);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800299 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800300}
301
302/**
303 * Initiates read-ahead of a page with given index.
304 *
305 * \retval +ve: page was added to \a queue.
306 *
307 * \retval -ENOLCK: there is no extent lock for this part of a file, stop
308 * read-ahead.
309 *
310 * \retval -ve, 0: page wasn't added to \a queue for other reason.
311 */
312static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
313 struct cl_page_list *queue,
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400314 pgoff_t index, pgoff_t *max_index)
Peng Taod7e09d02013-05-02 16:46:55 +0800315{
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400316 struct cl_object *clob = io->ci_obj;
John L. Hammond8c7b0e12016-03-30 19:48:47 -0400317 struct inode *inode = vvp_object_inode(clob);
Peng Taod7e09d02013-05-02 16:46:55 +0800318 struct page *vmpage;
Peng Taod7e09d02013-05-02 16:46:55 +0800319 struct cl_page *page;
320 enum ra_stat which = _NR_RA_STAT; /* keep gcc happy */
Peng Taod7e09d02013-05-02 16:46:55 +0800321 int rc = 0;
322 const char *msg = NULL;
323
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400324 vmpage = grab_cache_page_nowait(inode->i_mapping, index);
Oleg Drokin6e168182016-02-16 00:46:46 -0500325 if (vmpage) {
Peng Taod7e09d02013-05-02 16:46:55 +0800326 /* Check if vmpage was truncated or reclaimed */
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400327 if (vmpage->mapping == inode->i_mapping) {
Peng Taod7e09d02013-05-02 16:46:55 +0800328 page = cl_page_find(env, clob, vmpage->index,
329 vmpage, CPT_CACHEABLE);
330 if (!IS_ERR(page)) {
331 rc = cl_read_ahead_page(env, io, queue,
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400332 page, clob, max_index);
Peng Taod7e09d02013-05-02 16:46:55 +0800333 if (rc == -ENOLCK) {
334 which = RA_STAT_FAILED_MATCH;
335 msg = "lock match failed";
336 }
337 } else {
338 which = RA_STAT_FAILED_GRAB_PAGE;
339 msg = "cl_page_find failed";
340 }
341 } else {
342 which = RA_STAT_WRONG_GRAB_PAGE;
343 msg = "g_c_p_n returned invalid page";
344 }
345 if (rc != 1)
346 unlock_page(vmpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300347 put_page(vmpage);
Peng Taod7e09d02013-05-02 16:46:55 +0800348 } else {
349 which = RA_STAT_FAILED_GRAB_PAGE;
350 msg = "g_c_p_n failed";
351 }
Oleg Drokin6e168182016-02-16 00:46:46 -0500352 if (msg) {
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400353 ll_ra_stats_inc(inode, which);
Peng Taod7e09d02013-05-02 16:46:55 +0800354 CDEBUG(D_READA, "%s\n", msg);
355 }
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800356 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800357}
358
359#define RIA_DEBUG(ria) \
360 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
361 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
362 ria->ria_pages)
363
364/* Limit this to the blocksize instead of PTLRPC_BRW_MAX_SIZE, since we don't
365 * know what the actual RPC size is. If this needs to change, it makes more
366 * sense to tune the i_blkbits value for the file based on the OSTs it is
Oleg Drokinc0894c62016-02-24 22:00:30 -0500367 * striped over, rather than having a constant value for all files here.
368 */
Peng Taod7e09d02013-05-02 16:46:55 +0800369
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300370/* RAS_INCREASE_STEP should be (1UL << (inode->i_blkbits - PAGE_SHIFT)).
Masanari Iidad0a0acc2014-03-08 22:58:32 +0900371 * Temporarily set RAS_INCREASE_STEP to 1MB. After 4MB RPC is enabled
Peng Taod7e09d02013-05-02 16:46:55 +0800372 * by default, this should be adjusted corresponding with max_read_ahead_mb
373 * and max_read_ahead_per_file_mb otherwise the readahead budget can be used
Oleg Drokinc0894c62016-02-24 22:00:30 -0500374 * up quickly which will affect read performance significantly. See LU-2816
375 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300376#define RAS_INCREASE_STEP(inode) (ONE_MB_BRW_SIZE >> PAGE_SHIFT)
Peng Taod7e09d02013-05-02 16:46:55 +0800377
378static inline int stride_io_mode(struct ll_readahead_state *ras)
379{
380 return ras->ras_consecutive_stride_requests > 1;
381}
Mike Rapoportc9f6bb92015-10-13 16:03:42 +0300382
Peng Taod7e09d02013-05-02 16:46:55 +0800383/* The function calculates how much pages will be read in
384 * [off, off + length], in such stride IO area,
Masanari Iidad0a0acc2014-03-08 22:58:32 +0900385 * stride_offset = st_off, stride_length = st_len,
Peng Taod7e09d02013-05-02 16:46:55 +0800386 * stride_pages = st_pgs
387 *
388 * |------------------|*****|------------------|*****|------------|*****|....
389 * st_off
390 * |--- st_pgs ---|
391 * |----- st_len -----|
392 *
393 * How many pages it should read in such pattern
394 * |-------------------------------------------------------------|
395 * off
396 * |<------ length ------->|
397 *
398 * = |<----->| + |-------------------------------------| + |---|
399 * start_left st_pgs * i end_left
400 */
401static unsigned long
402stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
403 unsigned long off, unsigned long length)
404{
405 __u64 start = off > st_off ? off - st_off : 0;
406 __u64 end = off + length > st_off ? off + length - st_off : 0;
407 unsigned long start_left = 0;
408 unsigned long end_left = 0;
409 unsigned long pg_count;
410
411 if (st_len == 0 || length == 0 || end == 0)
412 return length;
413
414 start_left = do_div(start, st_len);
415 if (start_left < st_pgs)
416 start_left = st_pgs - start_left;
417 else
418 start_left = 0;
419
420 end_left = do_div(end, st_len);
421 if (end_left > st_pgs)
422 end_left = st_pgs;
423
Oleg Drokin72a87fc2016-02-24 22:00:40 -0500424 CDEBUG(D_READA, "start %llu, end %llu start_left %lu end_left %lu\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800425 start, end, start_left, end_left);
426
427 if (start == end)
428 pg_count = end_left - (st_pgs - start_left);
429 else
430 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
431
Joe Perches2d00bd12014-11-23 11:28:50 -0800432 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu pgcount %lu\n",
433 st_off, st_len, st_pgs, off, length, pg_count);
Peng Taod7e09d02013-05-02 16:46:55 +0800434
435 return pg_count;
436}
437
438static int ria_page_count(struct ra_io_arg *ria)
439{
440 __u64 length = ria->ria_end >= ria->ria_start ?
441 ria->ria_end - ria->ria_start + 1 : 0;
442
443 return stride_pg_count(ria->ria_stoff, ria->ria_length,
444 ria->ria_pages, ria->ria_start,
445 length);
446}
447
448/*Check whether the index is in the defined ra-window */
449static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
450{
451 /* If ria_length == ria_pages, it means non-stride I/O mode,
452 * idx should always inside read-ahead window in this case
453 * For stride I/O mode, just check whether the idx is inside
Oleg Drokinc0894c62016-02-24 22:00:30 -0500454 * the ria_pages.
455 */
Peng Taod7e09d02013-05-02 16:46:55 +0800456 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
457 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
458 ria->ria_length < ria->ria_pages);
459}
460
461static int ll_read_ahead_pages(const struct lu_env *env,
462 struct cl_io *io, struct cl_page_list *queue,
463 struct ra_io_arg *ria,
464 unsigned long *reserved_pages,
Peng Taod7e09d02013-05-02 16:46:55 +0800465 unsigned long *ra_end)
466{
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400467 int rc, count = 0;
468 bool stride_ria;
469 pgoff_t page_idx;
470 pgoff_t max_index = 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800471
Oleg Drokin6e168182016-02-16 00:46:46 -0500472 LASSERT(ria);
Peng Taod7e09d02013-05-02 16:46:55 +0800473 RIA_DEBUG(ria);
474
475 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
Oleg Drokine15ba452016-02-26 01:49:49 -0500476 for (page_idx = ria->ria_start;
477 page_idx <= ria->ria_end && *reserved_pages > 0; page_idx++) {
Peng Taod7e09d02013-05-02 16:46:55 +0800478 if (ras_inside_ra_window(page_idx, ria)) {
479 /* If the page is inside the read-ahead window*/
480 rc = ll_read_ahead_page(env, io, queue,
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400481 page_idx, &max_index);
Peng Taod7e09d02013-05-02 16:46:55 +0800482 if (rc == 1) {
483 (*reserved_pages)--;
Mike Rapoportb2952d62015-09-03 11:49:13 +0300484 count++;
Oleg Drokinda5ecb42016-04-01 15:18:01 -0400485 } else if (rc == -ENOLCK) {
Peng Taod7e09d02013-05-02 16:46:55 +0800486 break;
Oleg Drokinda5ecb42016-04-01 15:18:01 -0400487 }
Peng Taod7e09d02013-05-02 16:46:55 +0800488 } else if (stride_ria) {
489 /* If it is not in the read-ahead window, and it is
490 * read-ahead mode, then check whether it should skip
Oleg Drokinc0894c62016-02-24 22:00:30 -0500491 * the stride gap
492 */
Peng Taod7e09d02013-05-02 16:46:55 +0800493 pgoff_t offset;
494 /* FIXME: This assertion only is valid when it is for
495 * forward read-ahead, it will be fixed when backward
Oleg Drokinc0894c62016-02-24 22:00:30 -0500496 * read-ahead is implemented
497 */
Joe Perches2d00bd12014-11-23 11:28:50 -0800498 LASSERTF(page_idx > ria->ria_stoff, "Invalid page_idx %lu rs %lu re %lu ro %lu rl %lu rp %lu\n",
499 page_idx,
500 ria->ria_start, ria->ria_end, ria->ria_stoff,
501 ria->ria_length, ria->ria_pages);
Peng Taod7e09d02013-05-02 16:46:55 +0800502 offset = page_idx - ria->ria_stoff;
503 offset = offset % (ria->ria_length);
504 if (offset > ria->ria_pages) {
505 page_idx += ria->ria_length - offset;
Oleg Drokin72a87fc2016-02-24 22:00:40 -0500506 CDEBUG(D_READA, "i %lu skip %lu\n", page_idx,
Peng Taod7e09d02013-05-02 16:46:55 +0800507 ria->ria_length - offset);
508 continue;
509 }
510 }
511 }
512 *ra_end = page_idx;
513 return count;
514}
515
516int ll_readahead(const struct lu_env *env, struct cl_io *io,
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400517 struct cl_page_list *queue, struct ll_readahead_state *ras,
518 bool hit)
Peng Taod7e09d02013-05-02 16:46:55 +0800519{
520 struct vvp_io *vio = vvp_env_io(env);
John Hammond9989a582016-03-30 19:48:56 -0400521 struct ll_thread_info *lti = ll_env_info(env);
John Hammond9acc4502016-03-30 19:48:57 -0400522 struct cl_attr *attr = vvp_env_thread_attr(env);
Peng Taod7e09d02013-05-02 16:46:55 +0800523 unsigned long start = 0, end = 0, reserved;
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400524 unsigned long ra_end, len, mlen = 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800525 struct inode *inode;
John Hammond9989a582016-03-30 19:48:56 -0400526 struct ra_io_arg *ria = &lti->lti_ria;
Peng Taod7e09d02013-05-02 16:46:55 +0800527 struct cl_object *clob;
528 int ret = 0;
529 __u64 kms;
Peng Taod7e09d02013-05-02 16:46:55 +0800530
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400531 clob = io->ci_obj;
John L. Hammond8c7b0e12016-03-30 19:48:47 -0400532 inode = vvp_object_inode(clob);
Peng Taod7e09d02013-05-02 16:46:55 +0800533
Joe Perchesec83e612013-10-13 20:22:03 -0700534 memset(ria, 0, sizeof(*ria));
Peng Taod7e09d02013-05-02 16:46:55 +0800535
536 cl_object_attr_lock(clob);
537 ret = cl_object_attr_get(env, clob, attr);
538 cl_object_attr_unlock(clob);
539
540 if (ret != 0)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800541 return ret;
Peng Taod7e09d02013-05-02 16:46:55 +0800542 kms = attr->cat_kms;
543 if (kms == 0) {
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400544 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800545 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800546 }
547
548 spin_lock(&ras->ras_lock);
Peng Taod7e09d02013-05-02 16:46:55 +0800549
550 /* Enlarge the RA window to encompass the full read */
John L. Hammonde0a81442016-03-30 19:48:52 -0400551 if (vio->vui_ra_valid &&
John L. Hammondbc4320a2016-03-30 19:48:50 -0400552 ras->ras_window_start + ras->ras_window_len <
John L. Hammonde0a81442016-03-30 19:48:52 -0400553 vio->vui_ra_start + vio->vui_ra_count) {
554 ras->ras_window_len = vio->vui_ra_start + vio->vui_ra_count -
Peng Taod7e09d02013-05-02 16:46:55 +0800555 ras->ras_window_start;
556 }
John L. Hammondbc4320a2016-03-30 19:48:50 -0400557
Peng Taod7e09d02013-05-02 16:46:55 +0800558 /* Reserve a part of the read-ahead window that we'll be issuing */
559 if (ras->ras_window_len) {
560 start = ras->ras_next_readahead;
561 end = ras->ras_window_start + ras->ras_window_len - 1;
562 }
563 if (end != 0) {
564 unsigned long rpc_boundary;
565 /*
566 * Align RA window to an optimal boundary.
567 *
568 * XXX This would be better to align to cl_max_pages_per_rpc
569 * instead of PTLRPC_MAX_BRW_PAGES, because the RPC size may
570 * be aligned to the RAID stripe size in the future and that
571 * is more important than the RPC size.
572 */
573 /* Note: we only trim the RPC, instead of extending the RPC
574 * to the boundary, so to avoid reading too much pages during
Oleg Drokinc0894c62016-02-24 22:00:30 -0500575 * random reading.
576 */
Haneen Mohammedb6ee3822015-03-13 20:48:53 +0300577 rpc_boundary = (end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1));
Peng Taod7e09d02013-05-02 16:46:55 +0800578 if (rpc_boundary > 0)
579 rpc_boundary--;
580
581 if (rpc_boundary > start)
582 end = rpc_boundary;
583
584 /* Truncate RA window to end of file */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300585 end = min(end, (unsigned long)((kms - 1) >> PAGE_SHIFT));
Peng Taod7e09d02013-05-02 16:46:55 +0800586
587 ras->ras_next_readahead = max(end, end + 1);
588 RAS_CDEBUG(ras);
589 }
590 ria->ria_start = start;
591 ria->ria_end = end;
592 /* If stride I/O mode is detected, get stride window*/
593 if (stride_io_mode(ras)) {
594 ria->ria_stoff = ras->ras_stride_offset;
595 ria->ria_length = ras->ras_stride_length;
596 ria->ria_pages = ras->ras_stride_pages;
597 }
598 spin_unlock(&ras->ras_lock);
599
600 if (end == 0) {
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400601 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800602 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800603 }
604 len = ria_page_count(ria);
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400605 if (len == 0) {
606 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800607 return 0;
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400608 }
Peng Taod7e09d02013-05-02 16:46:55 +0800609
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400610 CDEBUG(D_READA, DFID ": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
611 PFID(lu_object_fid(&clob->co_lu)),
612 ria->ria_start, ria->ria_end,
John L. Hammonde0a81442016-03-30 19:48:52 -0400613 vio->vui_ra_valid ? vio->vui_ra_start : 0,
614 vio->vui_ra_valid ? vio->vui_ra_count : 0,
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400615 hit);
616
617 /* at least to extend the readahead window to cover current read */
John L. Hammonde0a81442016-03-30 19:48:52 -0400618 if (!hit && vio->vui_ra_valid &&
619 vio->vui_ra_start + vio->vui_ra_count > ria->ria_start) {
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400620 /* to the end of current read window. */
John L. Hammonde0a81442016-03-30 19:48:52 -0400621 mlen = vio->vui_ra_start + vio->vui_ra_count - ria->ria_start;
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400622 /* trim to RPC boundary */
623 start = ria->ria_start & (PTLRPC_MAX_BRW_PAGES - 1);
624 mlen = min(mlen, PTLRPC_MAX_BRW_PAGES - start);
625 }
626
627 reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len, mlen);
Peng Taod7e09d02013-05-02 16:46:55 +0800628 if (reserved < len)
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400629 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
Peng Taod7e09d02013-05-02 16:46:55 +0800630
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400631 CDEBUG(D_READA, "reserved pages %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
632 reserved, len, mlen,
Peng Taod7e09d02013-05-02 16:46:55 +0800633 atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
634 ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
635
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400636 ret = ll_read_ahead_pages(env, io, queue, ria, &reserved, &ra_end);
Peng Taod7e09d02013-05-02 16:46:55 +0800637
Peng Taod7e09d02013-05-02 16:46:55 +0800638 if (reserved != 0)
639 ll_ra_count_put(ll_i2sbi(inode), reserved);
640
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300641 if (ra_end == end + 1 && ra_end == (kms >> PAGE_SHIFT))
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400642 ll_ra_stats_inc(inode, RA_STAT_EOF);
Peng Taod7e09d02013-05-02 16:46:55 +0800643
644 /* if we didn't get to the end of the region we reserved from
645 * the ras we need to go back and update the ras so that the
646 * next read-ahead tries from where we left off. we only do so
647 * if the region we failed to issue read-ahead on is still ahead
Oleg Drokinc0894c62016-02-24 22:00:30 -0500648 * of the app and behind the next index to start read-ahead from
649 */
Oleg Drokin72a87fc2016-02-24 22:00:40 -0500650 CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800651 ra_end, end, ria->ria_end);
652
653 if (ra_end != end + 1) {
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400654 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
Peng Taod7e09d02013-05-02 16:46:55 +0800655 spin_lock(&ras->ras_lock);
656 if (ra_end < ras->ras_next_readahead &&
657 index_in_window(ra_end, ras->ras_window_start, 0,
658 ras->ras_window_len)) {
659 ras->ras_next_readahead = ra_end;
660 RAS_CDEBUG(ras);
661 }
662 spin_unlock(&ras->ras_lock);
663 }
664
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800665 return ret;
Peng Taod7e09d02013-05-02 16:46:55 +0800666}
667
668static void ras_set_start(struct inode *inode, struct ll_readahead_state *ras,
669 unsigned long index)
670{
671 ras->ras_window_start = index & (~(RAS_INCREASE_STEP(inode) - 1));
672}
673
674/* called with the ras_lock held or from places where it doesn't matter */
675static void ras_reset(struct inode *inode, struct ll_readahead_state *ras,
676 unsigned long index)
677{
678 ras->ras_last_readpage = index;
679 ras->ras_consecutive_requests = 0;
680 ras->ras_consecutive_pages = 0;
681 ras->ras_window_len = 0;
682 ras_set_start(inode, ras, index);
683 ras->ras_next_readahead = max(ras->ras_window_start, index);
684
685 RAS_CDEBUG(ras);
686}
687
688/* called with the ras_lock held or from places where it doesn't matter */
689static void ras_stride_reset(struct ll_readahead_state *ras)
690{
691 ras->ras_consecutive_stride_requests = 0;
692 ras->ras_stride_length = 0;
693 ras->ras_stride_pages = 0;
694 RAS_CDEBUG(ras);
695}
696
697void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
698{
699 spin_lock_init(&ras->ras_lock);
700 ras_reset(inode, ras, 0);
701 ras->ras_requests = 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800702}
703
704/*
705 * Check whether the read request is in the stride window.
706 * If it is in the stride window, return 1, otherwise return 0.
707 */
708static int index_in_stride_window(struct ll_readahead_state *ras,
709 unsigned long index)
710{
711 unsigned long stride_gap;
712
713 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
714 ras->ras_stride_pages == ras->ras_stride_length)
715 return 0;
716
717 stride_gap = index - ras->ras_last_readpage - 1;
718
719 /* If it is contiguous read */
720 if (stride_gap == 0)
721 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
722
723 /* Otherwise check the stride by itself */
724 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
725 ras->ras_consecutive_pages == ras->ras_stride_pages;
726}
727
728static void ras_update_stride_detector(struct ll_readahead_state *ras,
729 unsigned long index)
730{
731 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
732
733 if (!stride_io_mode(ras) && (stride_gap != 0 ||
Oleg Drokine15ba452016-02-26 01:49:49 -0500734 ras->ras_consecutive_stride_requests == 0)) {
Peng Taod7e09d02013-05-02 16:46:55 +0800735 ras->ras_stride_pages = ras->ras_consecutive_pages;
Mike Rapoportb2952d62015-09-03 11:49:13 +0300736 ras->ras_stride_length = stride_gap+ras->ras_consecutive_pages;
Peng Taod7e09d02013-05-02 16:46:55 +0800737 }
738 LASSERT(ras->ras_request_index == 0);
739 LASSERT(ras->ras_consecutive_stride_requests == 0);
740
741 if (index <= ras->ras_last_readpage) {
742 /*Reset stride window for forward read*/
743 ras_stride_reset(ras);
744 return;
745 }
746
747 ras->ras_stride_pages = ras->ras_consecutive_pages;
Mike Rapoportb2952d62015-09-03 11:49:13 +0300748 ras->ras_stride_length = stride_gap+ras->ras_consecutive_pages;
Peng Taod7e09d02013-05-02 16:46:55 +0800749
750 RAS_CDEBUG(ras);
751 return;
752}
753
Peng Taod7e09d02013-05-02 16:46:55 +0800754/* Stride Read-ahead window will be increased inc_len according to
Oleg Drokinc0894c62016-02-24 22:00:30 -0500755 * stride I/O pattern
756 */
Peng Taod7e09d02013-05-02 16:46:55 +0800757static void ras_stride_increase_window(struct ll_readahead_state *ras,
758 struct ll_ra_info *ra,
759 unsigned long inc_len)
760{
761 unsigned long left, step, window_len;
762 unsigned long stride_len;
763
764 LASSERT(ras->ras_stride_length > 0);
765 LASSERTF(ras->ras_window_start + ras->ras_window_len
Joe Perches2d00bd12014-11-23 11:28:50 -0800766 >= ras->ras_stride_offset, "window_start %lu, window_len %lu stride_offset %lu\n",
767 ras->ras_window_start,
Peng Taod7e09d02013-05-02 16:46:55 +0800768 ras->ras_window_len, ras->ras_stride_offset);
769
770 stride_len = ras->ras_window_start + ras->ras_window_len -
771 ras->ras_stride_offset;
772
773 left = stride_len % ras->ras_stride_length;
774 window_len = ras->ras_window_len - left;
775
776 if (left < ras->ras_stride_pages)
777 left += inc_len;
778 else
779 left = ras->ras_stride_pages + inc_len;
780
781 LASSERT(ras->ras_stride_pages != 0);
782
783 step = left / ras->ras_stride_pages;
784 left %= ras->ras_stride_pages;
785
786 window_len += step * ras->ras_stride_length + left;
787
Shivani Bhardwaj464e5942015-11-07 13:32:07 +0530788 if (stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
789 ras->ras_stride_pages, ras->ras_stride_offset,
790 window_len) <= ra->ra_max_pages_per_file)
Peng Taod7e09d02013-05-02 16:46:55 +0800791 ras->ras_window_len = window_len;
792
793 RAS_CDEBUG(ras);
794}
795
796static void ras_increase_window(struct inode *inode,
797 struct ll_readahead_state *ras,
798 struct ll_ra_info *ra)
799{
800 /* The stretch of ra-window should be aligned with max rpc_size
801 * but current clio architecture does not support retrieve such
802 * information from lower layer. FIXME later
803 */
804 if (stride_io_mode(ras))
805 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP(inode));
806 else
807 ras->ras_window_len = min(ras->ras_window_len +
808 RAS_INCREASE_STEP(inode),
809 ra->ra_max_pages_per_file);
810}
811
812void ras_update(struct ll_sb_info *sbi, struct inode *inode,
813 struct ll_readahead_state *ras, unsigned long index,
814 unsigned hit)
815{
816 struct ll_ra_info *ra = &sbi->ll_ra_info;
817 int zero = 0, stride_detect = 0, ra_miss = 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800818
819 spin_lock(&ras->ras_lock);
820
821 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
822
823 /* reset the read-ahead window in two cases. First when the app seeks
824 * or reads to some other part of the file. Secondly if we get a
825 * read-ahead miss that we think we've previously issued. This can
826 * be a symptom of there being so many read-ahead pages that the VM is
Oleg Drokinc0894c62016-02-24 22:00:30 -0500827 * reclaiming it before we get to it.
828 */
Peng Taod7e09d02013-05-02 16:46:55 +0800829 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
830 zero = 1;
831 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
832 } else if (!hit && ras->ras_window_len &&
833 index < ras->ras_next_readahead &&
834 index_in_window(index, ras->ras_window_start, 0,
835 ras->ras_window_len)) {
836 ra_miss = 1;
837 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
838 }
839
840 /* On the second access to a file smaller than the tunable
841 * ra_max_read_ahead_whole_pages trigger RA on all pages in the
842 * file up to ra_max_pages_per_file. This is simply a best effort
843 * and only occurs once per open file. Normal RA behavior is reverted
844 * to for subsequent IO. The mmap case does not increment
Oleg Drokinc0894c62016-02-24 22:00:30 -0500845 * ras_requests and thus can never trigger this behavior.
846 */
Peng Taod7e09d02013-05-02 16:46:55 +0800847 if (ras->ras_requests == 2 && !ras->ras_request_index) {
848 __u64 kms_pages;
849
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300850 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
851 PAGE_SHIFT;
Peng Taod7e09d02013-05-02 16:46:55 +0800852
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -0700853 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
Peng Taod7e09d02013-05-02 16:46:55 +0800854 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
855
856 if (kms_pages &&
857 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
858 ras->ras_window_start = 0;
859 ras->ras_last_readpage = 0;
860 ras->ras_next_readahead = 0;
861 ras->ras_window_len = min(ra->ra_max_pages_per_file,
862 ra->ra_max_read_ahead_whole_pages);
Julia Lawall34e1f2b2014-08-30 16:24:55 +0200863 goto out_unlock;
Peng Taod7e09d02013-05-02 16:46:55 +0800864 }
865 }
866 if (zero) {
867 /* check whether it is in stride I/O mode*/
868 if (!index_in_stride_window(ras, index)) {
869 if (ras->ras_consecutive_stride_requests == 0 &&
870 ras->ras_request_index == 0) {
871 ras_update_stride_detector(ras, index);
872 ras->ras_consecutive_stride_requests++;
873 } else {
874 ras_stride_reset(ras);
875 }
876 ras_reset(inode, ras, index);
877 ras->ras_consecutive_pages++;
Julia Lawall34e1f2b2014-08-30 16:24:55 +0200878 goto out_unlock;
Peng Taod7e09d02013-05-02 16:46:55 +0800879 } else {
880 ras->ras_consecutive_pages = 0;
881 ras->ras_consecutive_requests = 0;
882 if (++ras->ras_consecutive_stride_requests > 1)
883 stride_detect = 1;
884 RAS_CDEBUG(ras);
885 }
886 } else {
887 if (ra_miss) {
888 if (index_in_stride_window(ras, index) &&
889 stride_io_mode(ras)) {
890 /*If stride-RA hit cache miss, the stride dector
891 *will not be reset to avoid the overhead of
Oleg Drokinc0894c62016-02-24 22:00:30 -0500892 *redetecting read-ahead mode
893 */
Peng Taod7e09d02013-05-02 16:46:55 +0800894 if (index != ras->ras_last_readpage + 1)
895 ras->ras_consecutive_pages = 0;
896 ras_reset(inode, ras, index);
897 RAS_CDEBUG(ras);
898 } else {
899 /* Reset both stride window and normal RA
Oleg Drokinc0894c62016-02-24 22:00:30 -0500900 * window
901 */
Peng Taod7e09d02013-05-02 16:46:55 +0800902 ras_reset(inode, ras, index);
903 ras->ras_consecutive_pages++;
904 ras_stride_reset(ras);
Julia Lawall34e1f2b2014-08-30 16:24:55 +0200905 goto out_unlock;
Peng Taod7e09d02013-05-02 16:46:55 +0800906 }
907 } else if (stride_io_mode(ras)) {
908 /* If this is contiguous read but in stride I/O mode
909 * currently, check whether stride step still is valid,
Oleg Drokinc0894c62016-02-24 22:00:30 -0500910 * if invalid, it will reset the stride ra window
911 */
Peng Taod7e09d02013-05-02 16:46:55 +0800912 if (!index_in_stride_window(ras, index)) {
913 /* Shrink stride read-ahead window to be zero */
914 ras_stride_reset(ras);
915 ras->ras_window_len = 0;
916 ras->ras_next_readahead = index;
917 }
918 }
919 }
920 ras->ras_consecutive_pages++;
921 ras->ras_last_readpage = index;
922 ras_set_start(inode, ras, index);
923
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400924 if (stride_io_mode(ras)) {
Masanari Iidad0a0acc2014-03-08 22:58:32 +0900925 /* Since stride readahead is sensitive to the offset
Peng Taod7e09d02013-05-02 16:46:55 +0800926 * of read-ahead, so we use original offset here,
Oleg Drokinc0894c62016-02-24 22:00:30 -0500927 * instead of ras_window_start, which is RPC aligned
928 */
Peng Taod7e09d02013-05-02 16:46:55 +0800929 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
Jinshan Xiongfd7444f2016-03-30 19:48:33 -0400930 } else {
931 if (ras->ras_next_readahead < ras->ras_window_start)
932 ras->ras_next_readahead = ras->ras_window_start;
933 if (!hit)
934 ras->ras_next_readahead = index + 1;
935 }
Peng Taod7e09d02013-05-02 16:46:55 +0800936 RAS_CDEBUG(ras);
937
938 /* Trigger RA in the mmap case where ras_consecutive_requests
Oleg Drokinc0894c62016-02-24 22:00:30 -0500939 * is not incremented and thus can't be used to trigger RA
940 */
Peng Taod7e09d02013-05-02 16:46:55 +0800941 if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
942 ras->ras_window_len = RAS_INCREASE_STEP(inode);
Julia Lawall34e1f2b2014-08-30 16:24:55 +0200943 goto out_unlock;
Peng Taod7e09d02013-05-02 16:46:55 +0800944 }
945
946 /* Initially reset the stride window offset to next_readahead*/
947 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
948 /**
949 * Once stride IO mode is detected, next_readahead should be
950 * reset to make sure next_readahead > stride offset
951 */
952 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
953 ras->ras_stride_offset = index;
954 ras->ras_window_len = RAS_INCREASE_STEP(inode);
955 }
956
957 /* The initial ras_window_len is set to the request size. To avoid
958 * uselessly reading and discarding pages for random IO the window is
959 * only increased once per consecutive request received. */
960 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
961 !ras->ras_request_index)
962 ras_increase_window(inode, ras, ra);
Peng Taod7e09d02013-05-02 16:46:55 +0800963out_unlock:
964 RAS_CDEBUG(ras);
965 ras->ras_request_index++;
966 spin_unlock(&ras->ras_lock);
967 return;
968}
969
970int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
971{
972 struct inode *inode = vmpage->mapping->host;
973 struct ll_inode_info *lli = ll_i2info(inode);
974 struct lu_env *env;
975 struct cl_io *io;
976 struct cl_page *page;
977 struct cl_object *clob;
978 struct cl_env_nest nest;
979 bool redirtied = false;
980 bool unlocked = false;
981 int result;
Peng Taod7e09d02013-05-02 16:46:55 +0800982
983 LASSERT(PageLocked(vmpage));
984 LASSERT(!PageWriteback(vmpage));
985
Oleg Drokin6e168182016-02-16 00:46:46 -0500986 LASSERT(ll_i2dtexp(inode));
Peng Taod7e09d02013-05-02 16:46:55 +0800987
988 env = cl_env_nested_get(&nest);
Julia Lawall34e1f2b2014-08-30 16:24:55 +0200989 if (IS_ERR(env)) {
990 result = PTR_ERR(env);
991 goto out;
992 }
Peng Taod7e09d02013-05-02 16:46:55 +0800993
994 clob = ll_i2info(inode)->lli_clob;
Oleg Drokin6e168182016-02-16 00:46:46 -0500995 LASSERT(clob);
Peng Taod7e09d02013-05-02 16:46:55 +0800996
John Hammond9acc4502016-03-30 19:48:57 -0400997 io = vvp_env_thread_io(env);
Peng Taod7e09d02013-05-02 16:46:55 +0800998 io->ci_obj = clob;
999 io->ci_ignore_layout = 1;
1000 result = cl_io_init(env, io, CIT_MISC, clob);
1001 if (result == 0) {
1002 page = cl_page_find(env, clob, vmpage->index,
1003 vmpage, CPT_CACHEABLE);
1004 if (!IS_ERR(page)) {
1005 lu_ref_add(&page->cp_reference, "writepage",
1006 current);
1007 cl_page_assume(env, io, page);
1008 result = cl_page_flush(env, io, page);
1009 if (result != 0) {
1010 /*
1011 * Re-dirty page on error so it retries write,
1012 * but not in case when IO has actually
1013 * occurred and completed with an error.
1014 */
1015 if (!PageError(vmpage)) {
1016 redirty_page_for_writepage(wbc, vmpage);
1017 result = 0;
1018 redirtied = true;
1019 }
1020 }
1021 cl_page_disown(env, io, page);
1022 unlocked = true;
1023 lu_ref_del(&page->cp_reference,
1024 "writepage", current);
1025 cl_page_put(env, page);
1026 } else {
1027 result = PTR_ERR(page);
1028 }
1029 }
1030 cl_io_fini(env, io);
1031
1032 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1033 loff_t offset = cl_offset(clob, vmpage->index);
1034
1035 /* Flush page failed because the extent is being written out.
1036 * Wait for the write of extent to be finished to avoid
1037 * breaking kernel which assumes ->writepage should mark
Oleg Drokinc0894c62016-02-24 22:00:30 -05001038 * PageWriteback or clean the page.
1039 */
Peng Taod7e09d02013-05-02 16:46:55 +08001040 result = cl_sync_file_range(inode, offset,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001041 offset + PAGE_SIZE - 1,
Niu Yawei65fb55d2013-06-03 21:40:38 +08001042 CL_FSYNC_LOCAL, 1);
Peng Taod7e09d02013-05-02 16:46:55 +08001043 if (result > 0) {
1044 /* actually we may have written more than one page.
1045 * decreasing this page because the caller will count
Oleg Drokinc0894c62016-02-24 22:00:30 -05001046 * it.
1047 */
Peng Taod7e09d02013-05-02 16:46:55 +08001048 wbc->nr_to_write -= result - 1;
1049 result = 0;
1050 }
1051 }
1052
1053 cl_env_nested_put(&nest, env);
Julia Lawall34e1f2b2014-08-30 16:24:55 +02001054 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001055
1056out:
1057 if (result < 0) {
1058 if (!lli->lli_async_rc)
1059 lli->lli_async_rc = result;
1060 SetPageError(vmpage);
1061 if (!unlocked)
1062 unlock_page(vmpage);
1063 }
1064 return result;
1065}
1066
1067int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1068{
1069 struct inode *inode = mapping->host;
Niu Yawei65fb55d2013-06-03 21:40:38 +08001070 struct ll_sb_info *sbi = ll_i2sbi(inode);
Peng Taod7e09d02013-05-02 16:46:55 +08001071 loff_t start;
1072 loff_t end;
1073 enum cl_fsync_mode mode;
1074 int range_whole = 0;
1075 int result;
Niu Yawei65fb55d2013-06-03 21:40:38 +08001076 int ignore_layout = 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001077
1078 if (wbc->range_cyclic) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001079 start = mapping->writeback_index << PAGE_SHIFT;
Peng Taod7e09d02013-05-02 16:46:55 +08001080 end = OBD_OBJECT_EOF;
1081 } else {
1082 start = wbc->range_start;
1083 end = wbc->range_end;
1084 if (end == LLONG_MAX) {
1085 end = OBD_OBJECT_EOF;
1086 range_whole = start == 0;
1087 }
1088 }
1089
1090 mode = CL_FSYNC_NONE;
1091 if (wbc->sync_mode == WB_SYNC_ALL)
1092 mode = CL_FSYNC_LOCAL;
1093
Niu Yawei65fb55d2013-06-03 21:40:38 +08001094 if (sbi->ll_umounting)
1095 /* if the mountpoint is being umounted, all pages have to be
1096 * evicted to avoid hitting LBUG when truncate_inode_pages()
Oleg Drokinc0894c62016-02-24 22:00:30 -05001097 * is called later on.
1098 */
Niu Yawei65fb55d2013-06-03 21:40:38 +08001099 ignore_layout = 1;
1100 result = cl_sync_file_range(inode, start, end, mode, ignore_layout);
Peng Taod7e09d02013-05-02 16:46:55 +08001101 if (result > 0) {
1102 wbc->nr_to_write -= result;
1103 result = 0;
1104 }
1105
1106 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1107 if (end == OBD_OBJECT_EOF)
Jinshan Xiongc3558552016-04-27 18:20:53 -04001108 mapping->writeback_index = 0;
1109 else
1110 mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
Peng Taod7e09d02013-05-02 16:46:55 +08001111 }
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001112 return result;
Peng Taod7e09d02013-05-02 16:46:55 +08001113}
1114
1115int ll_readpage(struct file *file, struct page *vmpage)
1116{
1117 struct ll_cl_context *lcc;
1118 int result;
Peng Taod7e09d02013-05-02 16:46:55 +08001119
Jinshan Xiong77605e42016-03-30 19:48:30 -04001120 lcc = ll_cl_init(file, vmpage);
Peng Taod7e09d02013-05-02 16:46:55 +08001121 if (!IS_ERR(lcc)) {
1122 struct lu_env *env = lcc->lcc_env;
1123 struct cl_io *io = lcc->lcc_io;
1124 struct cl_page *page = lcc->lcc_page;
1125
1126 LASSERT(page->cp_type == CPT_CACHEABLE);
1127 if (likely(!PageUptodate(vmpage))) {
1128 cl_page_assume(env, io, page);
1129 result = cl_io_read_page(env, io, page);
1130 } else {
1131 /* Page from a non-object file. */
1132 unlock_page(vmpage);
1133 result = 0;
1134 }
1135 ll_cl_fini(lcc);
1136 } else {
1137 unlock_page(vmpage);
1138 result = PTR_ERR(lcc);
1139 }
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001140 return result;
Peng Taod7e09d02013-05-02 16:46:55 +08001141}
Jinshan Xiong77605e42016-03-30 19:48:30 -04001142
1143int ll_page_sync_io(const struct lu_env *env, struct cl_io *io,
1144 struct cl_page *page, enum cl_req_type crt)
1145{
1146 struct cl_2queue *queue;
1147 int result;
1148
1149 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
1150
1151 queue = &io->ci_queue;
1152 cl_2queue_init_page(queue, page);
1153
1154 result = cl_io_submit_sync(env, io, crt, queue, 0);
1155 LASSERT(cl_page_is_owned(page, io));
1156
1157 if (crt == CRT_READ)
1158 /*
1159 * in CRT_WRITE case page is left locked even in case of
1160 * error.
1161 */
1162 cl_page_list_disown(env, io, &queue->c2_qin);
1163 cl_2queue_fini(env, queue);
1164
1165 return result;
1166}