Whamcloud - gitweb
LU-3259 clio: Revise read ahead implementation
[fs/lustre-release.git] / lustre / llite / rw.c
1 /*
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  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
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>
48 #include <asm/uaccess.h>
49
50 #include <linux/fs.h>
51 #include <linux/stat.h>
52 #include <asm/uaccess.h>
53 #include <linux/mm.h>
54 #include <linux/pagemap.h>
55 /* current_is_kswapd() */
56 #include <linux/swap.h>
57
58 #define DEBUG_SUBSYSTEM S_LLITE
59
60 #include <obd_cksum.h>
61 #include "llite_internal.h"
62 #include <lustre_compat.h>
63
64 struct obd_capa *cl_capa_lookup(struct inode *inode, enum cl_req_type crt)
65 {
66         __u64 opc;
67
68         opc = crt == CRT_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
69         return ll_osscapa_get(inode, opc);
70 }
71
72 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
73
74 /**
75  * Get readahead pages from the filesystem readahead pool of the client for a
76  * thread.
77  *
78  * /param sbi superblock for filesystem readahead state ll_ra_info
79  * /param ria per-thread readahead state
80  * /param pages number of pages requested for readahead for the thread.
81  *
82  * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
83  * It should work well if the ra_max_pages is much greater than the single
84  * file's read-ahead window, and not too many threads contending for
85  * these readahead pages.
86  *
87  * TODO: There may be a 'global sync problem' if many threads are trying
88  * to get an ra budget that is larger than the remaining readahead pages
89  * and reach here at exactly the same time. They will compute /a ret to
90  * consume the remaining pages, but will fail at atomic_add_return() and
91  * get a zero ra window, although there is still ra space remaining. - Jay */
92
93 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
94                                      struct ra_io_arg *ria,
95                                      unsigned long pages, unsigned long min)
96 {
97         struct ll_ra_info *ra = &sbi->ll_ra_info;
98         long ret;
99         ENTRY;
100
101         /* If read-ahead pages left are less than 1M, do not do read-ahead,
102          * otherwise it will form small read RPC(< 1M), which hurt server
103          * performance a lot. */
104         ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages),
105                   pages);
106         if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages))
107                 GOTO(out, ret = 0);
108
109         /* If the non-strided (ria_pages == 0) readahead window
110          * (ria_start + ret) has grown across an RPC boundary, then trim
111          * readahead size by the amount beyond the RPC so it ends on an
112          * RPC boundary. If the readahead window is already ending on
113          * an RPC boundary (beyond_rpc == 0), or smaller than a full
114          * RPC (beyond_rpc < ret) the readahead size is unchanged.
115          * The (beyond_rpc != 0) check is skipped since the conditional
116          * branch is more expensive than subtracting zero from the result.
117          *
118          * Strided read is left unaligned to avoid small fragments beyond
119          * the RPC boundary from needing an extra read RPC. */
120         if (ria->ria_pages == 0) {
121                 long beyond_rpc = (ria->ria_start + ret) % PTLRPC_MAX_BRW_PAGES;
122                 if (/* beyond_rpc != 0 && */ beyond_rpc < ret)
123                         ret -= beyond_rpc;
124         }
125
126         if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
127                 atomic_sub(ret, &ra->ra_cur_pages);
128                 ret = 0;
129         }
130
131 out:
132         if (ret < min) {
133                 /* override ra limit for maximum performance */
134                 atomic_add(min - ret, &ra->ra_cur_pages);
135                 ret = min;
136         }
137         RETURN(ret);
138 }
139
140 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
141 {
142         struct ll_ra_info *ra = &sbi->ll_ra_info;
143         atomic_sub(len, &ra->ra_cur_pages);
144 }
145
146 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
147 {
148         LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
149         lprocfs_counter_incr(sbi->ll_ra_stats, which);
150 }
151
152 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
153 {
154         struct ll_sb_info *sbi = ll_i2sbi(inode);
155         ll_ra_stats_inc_sbi(sbi, which);
156 }
157
158 #define RAS_CDEBUG(ras) \
159         CDEBUG(D_READA,                                                      \
160                "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu"    \
161                "csr %lu sf %lu sp %lu sl %lu \n",                            \
162                ras->ras_last_readpage, ras->ras_consecutive_requests,        \
163                ras->ras_consecutive_pages, ras->ras_window_start,            \
164                ras->ras_window_len, ras->ras_next_readahead,                 \
165                ras->ras_requests, ras->ras_request_index,                    \
166                ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
167                ras->ras_stride_pages, ras->ras_stride_length)
168
169 static int index_in_window(unsigned long index, unsigned long point,
170                            unsigned long before, unsigned long after)
171 {
172         unsigned long start = point - before, end = point + after;
173
174         if (start > point)
175                start = 0;
176         if (end < point)
177                end = ~0;
178
179         return start <= index && index <= end;
180 }
181
182 void ll_ras_enter(struct file *f)
183 {
184         struct ll_file_data *fd = LUSTRE_FPRIVATE(f);
185         struct ll_readahead_state *ras = &fd->fd_ras;
186
187         spin_lock(&ras->ras_lock);
188         ras->ras_requests++;
189         ras->ras_request_index = 0;
190         ras->ras_consecutive_requests++;
191         spin_unlock(&ras->ras_lock);
192 }
193
194 /**
195  * Initiates read-ahead of a page with given index.
196  *
197  * \retval +ve: page was already uptodate so it will be skipped
198  *              from being added;
199  * \retval -ve: page wasn't added to \a queue for error;
200  * \retval   0: page was added into \a queue for read ahead.
201  */
202 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
203                               struct cl_page_list *queue, pgoff_t index)
204 {
205         struct cl_object *clob  = io->ci_obj;
206         struct inode     *inode = vvp_object_inode(clob);
207         struct page      *vmpage;
208         struct cl_page   *page;
209         struct vvp_page  *vpg;
210         enum ra_stat      which = _NR_RA_STAT; /* keep gcc happy */
211         int               rc    = 0;
212         const char       *msg   = NULL;
213         ENTRY;
214
215         vmpage = grab_cache_page_nowait(inode->i_mapping, index);
216         if (vmpage == NULL) {
217                 which = RA_STAT_FAILED_GRAB_PAGE;
218                 msg   = "g_c_p_n failed";
219                 GOTO(out, rc = -EBUSY);
220         }
221
222         /* Check if vmpage was truncated or reclaimed */
223         if (vmpage->mapping != inode->i_mapping) {
224                 which = RA_STAT_WRONG_GRAB_PAGE;
225                 msg   = "g_c_p_n returned invalid page";
226                 GOTO(out, rc = -EBUSY);
227         }
228
229         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
230         if (IS_ERR(page)) {
231                 which = RA_STAT_FAILED_GRAB_PAGE;
232                 msg   = "cl_page_find failed";
233                 GOTO(out, rc = PTR_ERR(page));
234         }
235
236         lu_ref_add(&page->cp_reference, "ra", current);
237         cl_page_assume(env, io, page);
238         vpg = cl2vvp_page(cl_object_page_slice(clob, page));
239         if (!vpg->vpg_defer_uptodate && !PageUptodate(vmpage)) {
240                 vpg->vpg_defer_uptodate = 1;
241                 vpg->vpg_ra_used = 0;
242                 cl_page_list_add(queue, page);
243         } else {
244                 /* skip completed pages */
245                 cl_page_unassume(env, io, page);
246                 /* This page is already uptodate, returning a positive number
247                  * to tell the callers about this */
248                 rc = 1;
249         }
250
251         lu_ref_del(&page->cp_reference, "ra", current);
252         cl_page_put(env, page);
253
254 out:
255         if (vmpage != NULL) {
256                 if (rc != 0)
257                         unlock_page(vmpage);
258                 page_cache_release(vmpage);
259         }
260         if (msg != NULL) {
261                 ll_ra_stats_inc(inode, which);
262                 CDEBUG(D_READA, "%s\n", msg);
263
264         }
265
266         RETURN(rc);
267 }
268
269 #define RIA_DEBUG(ria)                                                       \
270         CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n",       \
271         ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
272         ria->ria_pages)
273
274 /* Limit this to the blocksize instead of PTLRPC_BRW_MAX_SIZE, since we don't
275  * know what the actual RPC size is.  If this needs to change, it makes more
276  * sense to tune the i_blkbits value for the file based on the OSTs it is
277  * striped over, rather than having a constant value for all files here. */
278
279 /* RAS_INCREASE_STEP should be (1UL << (inode->i_blkbits - PAGE_CACHE_SHIFT)).
280  * Temprarily set RAS_INCREASE_STEP to 1MB. After 4MB RPC is enabled
281  * by default, this should be adjusted corresponding with max_read_ahead_mb
282  * and max_read_ahead_per_file_mb otherwise the readahead budget can be used
283  * up quickly which will affect read performance siginificantly. See LU-2816 */
284 #define RAS_INCREASE_STEP(inode) (ONE_MB_BRW_SIZE >> PAGE_CACHE_SHIFT)
285
286 static inline int stride_io_mode(struct ll_readahead_state *ras)
287 {
288         return ras->ras_consecutive_stride_requests > 1;
289 }
290 /* The function calculates how much pages will be read in
291  * [off, off + length], in such stride IO area,
292  * stride_offset = st_off, stride_lengh = st_len,
293  * stride_pages = st_pgs
294  *
295  *   |------------------|*****|------------------|*****|------------|*****|....
296  * st_off
297  *   |--- st_pgs     ---|
298  *   |-----     st_len   -----|
299  *
300  *              How many pages it should read in such pattern
301  *              |-------------------------------------------------------------|
302  *              off
303  *              |<------                  length                      ------->|
304  *
305  *          =   |<----->|  +  |-------------------------------------| +   |---|
306  *             start_left                 st_pgs * i                    end_left
307  */
308 static unsigned long
309 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
310                 unsigned long off, unsigned long length)
311 {
312         __u64 start = off > st_off ? off - st_off : 0;
313         __u64 end = off + length > st_off ? off + length - st_off : 0;
314         unsigned long start_left = 0;
315         unsigned long end_left = 0;
316         unsigned long pg_count;
317
318         if (st_len == 0 || length == 0 || end == 0)
319                 return length;
320
321         start_left = do_div(start, st_len);
322         if (start_left < st_pgs)
323                 start_left = st_pgs - start_left;
324         else
325                 start_left = 0;
326
327         end_left = do_div(end, st_len);
328         if (end_left > st_pgs)
329                 end_left = st_pgs;
330
331         CDEBUG(D_READA, "start "LPU64", end "LPU64" start_left %lu end_left %lu \n",
332                start, end, start_left, end_left);
333
334         if (start == end)
335                 pg_count = end_left - (st_pgs - start_left);
336         else
337                 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
338
339         CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
340                "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
341
342         return pg_count;
343 }
344
345 static int ria_page_count(struct ra_io_arg *ria)
346 {
347         __u64 length = ria->ria_end >= ria->ria_start ?
348                        ria->ria_end - ria->ria_start + 1 : 0;
349
350         return stride_pg_count(ria->ria_stoff, ria->ria_length,
351                                ria->ria_pages, ria->ria_start,
352                                length);
353 }
354
355 /*Check whether the index is in the defined ra-window */
356 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
357 {
358         /* If ria_length == ria_pages, it means non-stride I/O mode,
359          * idx should always inside read-ahead window in this case
360          * For stride I/O mode, just check whether the idx is inside
361          * the ria_pages. */
362         return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
363                (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
364                 ria->ria_length < ria->ria_pages);
365 }
366
367 static int ll_read_ahead_pages(const struct lu_env *env,
368                                struct cl_io *io, struct cl_page_list *queue,
369                                struct ra_io_arg *ria,
370                                unsigned long *reserved_pages,
371                                pgoff_t *ra_end)
372 {
373         struct cl_read_ahead ra = { 0 };
374         int rc, count = 0;
375         bool stride_ria;
376         pgoff_t page_idx;
377
378         LASSERT(ria != NULL);
379         RIA_DEBUG(ria);
380
381         stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
382         for (page_idx = ria->ria_start;
383              page_idx <= ria->ria_end && *reserved_pages > 0; page_idx++) {
384                 if (ras_inside_ra_window(page_idx, ria)) {
385                         if (ra.cra_end == 0 || ra.cra_end < page_idx) {
386                                 cl_read_ahead_release(env, &ra);
387
388                                 rc = cl_io_read_ahead(env, io, page_idx, &ra);
389                                 if (rc < 0)
390                                         break;
391
392                                 LASSERTF(ra.cra_end >= page_idx,
393                                          "object: %p, indcies %lu / %lu\n",
394                                          io->ci_obj, ra.cra_end, page_idx);
395                         }
396
397                         /* If the page is inside the read-ahead window*/
398                         rc = ll_read_ahead_page(env, io, queue, page_idx);
399                         if (rc == 0) {
400                                 (*reserved_pages)--;
401                                 count++;
402                         }
403                 } else if (stride_ria) {
404                         /* If it is not in the read-ahead window, and it is
405                          * read-ahead mode, then check whether it should skip
406                          * the stride gap */
407                         pgoff_t offset;
408                         /* FIXME: This assertion only is valid when it is for
409                          * forward read-ahead, it will be fixed when backward
410                          * read-ahead is implemented */
411                         LASSERTF(page_idx >= ria->ria_stoff,
412                                 "Invalid page_idx %lu rs %lu re %lu ro %lu "
413                                 "rl %lu rp %lu\n", page_idx,
414                                 ria->ria_start, ria->ria_end, ria->ria_stoff,
415                                 ria->ria_length, ria->ria_pages);
416                         offset = page_idx - ria->ria_stoff;
417                         offset = offset % (ria->ria_length);
418                         if (offset > ria->ria_pages) {
419                                 page_idx += ria->ria_length - offset;
420                                 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
421                                        ria->ria_length - offset);
422                                 continue;
423                         }
424                 }
425         }
426
427         cl_read_ahead_release(env, &ra);
428
429         *ra_end = page_idx;
430         return count;
431 }
432
433 static int ll_readahead(const struct lu_env *env, struct cl_io *io,
434                         struct cl_page_list *queue,
435                         struct ll_readahead_state *ras, bool hit)
436 {
437         struct vvp_io *vio = vvp_env_io(env);
438         struct vvp_thread_info *vti = vvp_env_info(env);
439         struct cl_attr *attr = ccc_env_thread_attr(env);
440         unsigned long len, mlen = 0, reserved;
441         pgoff_t ra_end, start = 0, end = 0;
442         struct inode *inode;
443         struct ra_io_arg *ria = &vti->vti_ria;
444         struct cl_object *clob;
445         int ret = 0;
446         __u64 kms;
447         ENTRY;
448
449         clob = io->ci_obj;
450         inode = vvp_object_inode(clob);
451
452         memset(ria, 0, sizeof *ria);
453
454         cl_object_attr_lock(clob);
455         ret = cl_object_attr_get(env, clob, attr);
456         cl_object_attr_unlock(clob);
457
458         if (ret != 0)
459                 RETURN(ret);
460         kms = attr->cat_kms;
461         if (kms == 0) {
462                 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
463                 RETURN(0);
464         }
465
466         spin_lock(&ras->ras_lock);
467
468         /* Enlarge the RA window to encompass the full read */
469         if (vio->vui_ra_valid &&
470             ras->ras_window_start + ras->ras_window_len <
471             vio->vui_ra_start + vio->vui_ra_count) {
472                 ras->ras_window_len = vio->vui_ra_start + vio->vui_ra_count -
473                         ras->ras_window_start;
474         }
475
476         /* Reserve a part of the read-ahead window that we'll be issuing */
477         if (ras->ras_window_len > 0) {
478                 /*
479                  * Note: other thread might rollback the ras_next_readahead,
480                  * if it can not get the full size of prepared pages, see the
481                  * end of this function. For stride read ahead, it needs to
482                  * make sure the offset is no less than ras_stride_offset,
483                  * so that stride read ahead can work correctly.
484                  */
485                 if (stride_io_mode(ras))
486                         start = max(ras->ras_next_readahead,
487                                     ras->ras_stride_offset);
488                 else
489                         start = ras->ras_next_readahead;
490                 end = ras->ras_window_start + ras->ras_window_len - 1;
491         }
492
493         if (end != 0) {
494                 unsigned long rpc_boundary;
495                 /*
496                  * Align RA window to an optimal boundary.
497                  *
498                  * XXX This would be better to align to cl_max_pages_per_rpc
499                  * instead of PTLRPC_MAX_BRW_PAGES, because the RPC size may
500                  * be aligned to the RAID stripe size in the future and that
501                  * is more important than the RPC size.
502                  */
503                 /* Note: we only trim the RPC, instead of extending the RPC
504                  * to the boundary, so to avoid reading too much pages during
505                  * random reading. */
506                 rpc_boundary = ((end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1)));
507                 if (rpc_boundary > 0)
508                         rpc_boundary--;
509
510                 if (rpc_boundary  > start)
511                         end = rpc_boundary;
512
513                 /* Truncate RA window to end of file */
514                 end = min(end, (unsigned long)((kms - 1) >> PAGE_CACHE_SHIFT));
515
516                 ras->ras_next_readahead = max(end, end + 1);
517                 RAS_CDEBUG(ras);
518         }
519         ria->ria_start = start;
520         ria->ria_end = end;
521         /* If stride I/O mode is detected, get stride window*/
522         if (stride_io_mode(ras)) {
523                 ria->ria_stoff = ras->ras_stride_offset;
524                 ria->ria_length = ras->ras_stride_length;
525                 ria->ria_pages = ras->ras_stride_pages;
526         }
527         spin_unlock(&ras->ras_lock);
528
529         if (end == 0) {
530                 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
531                 RETURN(0);
532         }
533         len = ria_page_count(ria);
534         if (len == 0) {
535                 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
536                 RETURN(0);
537         }
538
539         CDEBUG(D_READA, DFID": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
540                PFID(lu_object_fid(&clob->co_lu)),
541                ria->ria_start, ria->ria_end,
542                vio->vui_ra_valid ? vio->vui_ra_start : 0,
543                vio->vui_ra_valid ? vio->vui_ra_count : 0,
544                hit);
545
546         /* at least to extend the readahead window to cover current read */
547         if (!hit && vio->vui_ra_valid &&
548             vio->vui_ra_start + vio->vui_ra_count > ria->ria_start) {
549                 /* to the end of current read window. */
550                 mlen = vio->vui_ra_start + vio->vui_ra_count - ria->ria_start;
551                 /* trim to RPC boundary */
552                 start = ria->ria_start & (PTLRPC_MAX_BRW_PAGES - 1);
553                 mlen = min(mlen, PTLRPC_MAX_BRW_PAGES - start);
554         }
555
556         reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len, mlen);
557         if (reserved < len)
558                 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
559
560         CDEBUG(D_READA, "reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
561                reserved, len, mlen,
562                atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
563                ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
564
565         ret = ll_read_ahead_pages(env, io, queue, ria, &reserved, &ra_end);
566
567         if (reserved != 0)
568                 ll_ra_count_put(ll_i2sbi(inode), reserved);
569
570         if (ra_end == end + 1 && ra_end == (kms >> PAGE_CACHE_SHIFT))
571                 ll_ra_stats_inc(inode, RA_STAT_EOF);
572
573         /* if we didn't get to the end of the region we reserved from
574          * the ras we need to go back and update the ras so that the
575          * next read-ahead tries from where we left off.  we only do so
576          * if the region we failed to issue read-ahead on is still ahead
577          * of the app and behind the next index to start read-ahead from */
578         CDEBUG(D_READA, "ra_end = %lu end = %lu stride end = %lu pages = %d\n",
579                ra_end, end, ria->ria_end, ret);
580
581         if (ra_end != end + 1) {
582                 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
583                 spin_lock(&ras->ras_lock);
584                 if (ra_end < ras->ras_next_readahead &&
585                     index_in_window(ra_end, ras->ras_window_start, 0,
586                                     ras->ras_window_len)) {
587                         ras->ras_next_readahead = ra_end;
588                         RAS_CDEBUG(ras);
589                 }
590                 spin_unlock(&ras->ras_lock);
591         }
592
593         RETURN(ret);
594 }
595
596 static void ras_set_start(struct inode *inode, struct ll_readahead_state *ras,
597                           unsigned long index)
598 {
599         ras->ras_window_start = index & (~(RAS_INCREASE_STEP(inode) - 1));
600 }
601
602 /* called with the ras_lock held or from places where it doesn't matter */
603 static void ras_reset(struct inode *inode, struct ll_readahead_state *ras,
604                       unsigned long index)
605 {
606         ras->ras_last_readpage = index;
607         ras->ras_consecutive_requests = 0;
608         ras->ras_consecutive_pages = 0;
609         ras->ras_window_len = 0;
610         ras_set_start(inode, ras, index);
611         ras->ras_next_readahead = max(ras->ras_window_start, index);
612
613         RAS_CDEBUG(ras);
614 }
615
616 /* called with the ras_lock held or from places where it doesn't matter */
617 static void ras_stride_reset(struct ll_readahead_state *ras)
618 {
619         ras->ras_consecutive_stride_requests = 0;
620         ras->ras_stride_length = 0;
621         ras->ras_stride_pages = 0;
622         RAS_CDEBUG(ras);
623 }
624
625 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
626 {
627         spin_lock_init(&ras->ras_lock);
628         ras_reset(inode, ras, 0);
629         ras->ras_requests = 0;
630 }
631
632 /*
633  * Check whether the read request is in the stride window.
634  * If it is in the stride window, return 1, otherwise return 0.
635  */
636 static int index_in_stride_window(struct ll_readahead_state *ras,
637                                   unsigned long index)
638 {
639         unsigned long stride_gap;
640
641         if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
642             ras->ras_stride_pages == ras->ras_stride_length)
643                 return 0;
644
645         stride_gap = index - ras->ras_last_readpage - 1;
646
647         /* If it is contiguous read */
648         if (stride_gap == 0)
649                 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
650
651         /* Otherwise check the stride by itself */
652         return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
653                 ras->ras_consecutive_pages == ras->ras_stride_pages;
654 }
655
656 static void ras_update_stride_detector(struct ll_readahead_state *ras,
657                                        unsigned long index)
658 {
659         unsigned long stride_gap = index - ras->ras_last_readpage - 1;
660
661         if (!stride_io_mode(ras) && (stride_gap != 0 ||
662              ras->ras_consecutive_stride_requests == 0)) {
663                 ras->ras_stride_pages = ras->ras_consecutive_pages;
664                 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
665         }
666         LASSERT(ras->ras_request_index == 0);
667         LASSERT(ras->ras_consecutive_stride_requests == 0);
668
669         if (index <= ras->ras_last_readpage) {
670                 /*Reset stride window for forward read*/
671                 ras_stride_reset(ras);
672                 return;
673         }
674
675         ras->ras_stride_pages = ras->ras_consecutive_pages;
676         ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
677
678         RAS_CDEBUG(ras);
679         return;
680 }
681
682 static unsigned long
683 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
684 {
685         return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
686                                ras->ras_stride_pages, ras->ras_stride_offset,
687                                len);
688 }
689
690 /* Stride Read-ahead window will be increased inc_len according to
691  * stride I/O pattern */
692 static void ras_stride_increase_window(struct ll_readahead_state *ras,
693                                        struct ll_ra_info *ra,
694                                        unsigned long inc_len)
695 {
696         unsigned long left, step, window_len;
697         unsigned long stride_len;
698
699         LASSERT(ras->ras_stride_length > 0);
700         LASSERTF(ras->ras_window_start + ras->ras_window_len
701                  >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
702                  " stride_offset %lu\n", ras->ras_window_start,
703                  ras->ras_window_len, ras->ras_stride_offset);
704
705         stride_len = ras->ras_window_start + ras->ras_window_len -
706                      ras->ras_stride_offset;
707
708         left = stride_len % ras->ras_stride_length;
709         window_len = ras->ras_window_len - left;
710
711         if (left < ras->ras_stride_pages)
712                 left += inc_len;
713         else
714                 left = ras->ras_stride_pages + inc_len;
715
716         LASSERT(ras->ras_stride_pages != 0);
717
718         step = left / ras->ras_stride_pages;
719         left %= ras->ras_stride_pages;
720
721         window_len += step * ras->ras_stride_length + left;
722
723         if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
724                 ras->ras_window_len = window_len;
725
726         RAS_CDEBUG(ras);
727 }
728
729 static void ras_increase_window(struct inode *inode,
730                                 struct ll_readahead_state *ras,
731                                 struct ll_ra_info *ra)
732 {
733         /* The stretch of ra-window should be aligned with max rpc_size
734          * but current clio architecture does not support retrieve such
735          * information from lower layer. FIXME later
736          */
737         if (stride_io_mode(ras))
738                 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP(inode));
739         else
740                 ras->ras_window_len = min(ras->ras_window_len +
741                                           RAS_INCREASE_STEP(inode),
742                                           ra->ra_max_pages_per_file);
743 }
744
745 static void ras_update(struct ll_sb_info *sbi, struct inode *inode,
746                        struct ll_readahead_state *ras, unsigned long index,
747                        unsigned hit)
748 {
749         struct ll_ra_info *ra = &sbi->ll_ra_info;
750         int zero = 0, stride_detect = 0, ra_miss = 0;
751         ENTRY;
752
753         spin_lock(&ras->ras_lock);
754
755         ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
756
757         /* reset the read-ahead window in two cases.  First when the app seeks
758          * or reads to some other part of the file.  Secondly if we get a
759          * read-ahead miss that we think we've previously issued.  This can
760          * be a symptom of there being so many read-ahead pages that the VM is
761          * reclaiming it before we get to it. */
762         if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
763                 zero = 1;
764                 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
765         } else if (!hit && ras->ras_window_len &&
766                    index < ras->ras_next_readahead &&
767                    index_in_window(index, ras->ras_window_start, 0,
768                                    ras->ras_window_len)) {
769                 ra_miss = 1;
770                 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
771         }
772
773         /* On the second access to a file smaller than the tunable
774          * ra_max_read_ahead_whole_pages trigger RA on all pages in the
775          * file up to ra_max_pages_per_file.  This is simply a best effort
776          * and only occurs once per open file.  Normal RA behavior is reverted
777          * to for subsequent IO.  The mmap case does not increment
778          * ras_requests and thus can never trigger this behavior. */
779         if (ras->ras_requests == 2 && !ras->ras_request_index) {
780                 __u64 kms_pages;
781
782                 kms_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
783                             PAGE_CACHE_SHIFT;
784
785                 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
786                        ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
787
788                 if (kms_pages &&
789                     kms_pages <= ra->ra_max_read_ahead_whole_pages) {
790                         ras->ras_window_start = 0;
791                         ras->ras_last_readpage = 0;
792                         ras->ras_next_readahead = 0;
793                         ras->ras_window_len = min(ra->ra_max_pages_per_file,
794                                 ra->ra_max_read_ahead_whole_pages);
795                         GOTO(out_unlock, 0);
796                 }
797         }
798         if (zero) {
799                 /* check whether it is in stride I/O mode*/
800                 if (!index_in_stride_window(ras, index)) {
801                         if (ras->ras_consecutive_stride_requests == 0 &&
802                             ras->ras_request_index == 0) {
803                                 ras_update_stride_detector(ras, index);
804                                 ras->ras_consecutive_stride_requests++;
805                         } else {
806                                 ras_stride_reset(ras);
807                         }
808                         ras_reset(inode, ras, index);
809                         ras->ras_consecutive_pages++;
810                         GOTO(out_unlock, 0);
811                 } else {
812                         ras->ras_consecutive_pages = 0;
813                         ras->ras_consecutive_requests = 0;
814                         if (++ras->ras_consecutive_stride_requests > 1)
815                                 stride_detect = 1;
816                         RAS_CDEBUG(ras);
817                 }
818         } else {
819                 if (ra_miss) {
820                         if (index_in_stride_window(ras, index) &&
821                             stride_io_mode(ras)) {
822                                 /*If stride-RA hit cache miss, the stride dector
823                                  *will not be reset to avoid the overhead of
824                                  *redetecting read-ahead mode */
825                                 if (index != ras->ras_last_readpage + 1)
826                                         ras->ras_consecutive_pages = 0;
827                                 ras_reset(inode, ras, index);
828                                 RAS_CDEBUG(ras);
829                         } else {
830                                 /* Reset both stride window and normal RA
831                                  * window */
832                                 ras_reset(inode, ras, index);
833                                 ras->ras_consecutive_pages++;
834                                 ras_stride_reset(ras);
835                                 GOTO(out_unlock, 0);
836                         }
837                 } else if (stride_io_mode(ras)) {
838                         /* If this is contiguous read but in stride I/O mode
839                          * currently, check whether stride step still is valid,
840                          * if invalid, it will reset the stride ra window*/
841                         if (!index_in_stride_window(ras, index)) {
842                                 /* Shrink stride read-ahead window to be zero */
843                                 ras_stride_reset(ras);
844                                 ras->ras_window_len = 0;
845                                 ras->ras_next_readahead = index;
846                         }
847                 }
848         }
849         ras->ras_consecutive_pages++;
850         ras->ras_last_readpage = index;
851         ras_set_start(inode, ras, index);
852
853         if (stride_io_mode(ras)) {
854                 /* Since stride readahead is sentivite to the offset
855                  * of read-ahead, so we use original offset here,
856                  * instead of ras_window_start, which is RPC aligned */
857                 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
858         } else {
859                 if (ras->ras_next_readahead < ras->ras_window_start)
860                         ras->ras_next_readahead = ras->ras_window_start;
861                 if (!hit)
862                         ras->ras_next_readahead = index + 1;
863         }
864         RAS_CDEBUG(ras);
865
866         /* Trigger RA in the mmap case where ras_consecutive_requests
867          * is not incremented and thus can't be used to trigger RA */
868         if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
869                 ras->ras_window_len = RAS_INCREASE_STEP(inode);
870                 GOTO(out_unlock, 0);
871         }
872
873         /* Initially reset the stride window offset to next_readahead*/
874         if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
875                 /**
876                  * Once stride IO mode is detected, next_readahead should be
877                  * reset to make sure next_readahead > stride offset
878                  */
879                 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
880                 ras->ras_stride_offset = index;
881                 ras->ras_window_len = RAS_INCREASE_STEP(inode);
882         }
883
884         /* The initial ras_window_len is set to the request size.  To avoid
885          * uselessly reading and discarding pages for random IO the window is
886          * only increased once per consecutive request received. */
887         if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
888             !ras->ras_request_index)
889                 ras_increase_window(inode, ras, ra);
890         EXIT;
891 out_unlock:
892         RAS_CDEBUG(ras);
893         ras->ras_request_index++;
894         spin_unlock(&ras->ras_lock);
895         return;
896 }
897
898 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
899 {
900         struct inode           *inode = vmpage->mapping->host;
901         struct ll_inode_info   *lli   = ll_i2info(inode);
902         struct lu_env          *env;
903         struct cl_io           *io;
904         struct cl_page         *page;
905         struct cl_object       *clob;
906         struct cl_env_nest      nest;
907         bool redirtied = false;
908         bool unlocked = false;
909         int result;
910         ENTRY;
911
912         LASSERT(PageLocked(vmpage));
913         LASSERT(!PageWriteback(vmpage));
914
915         LASSERT(ll_i2dtexp(inode) != NULL);
916
917         env = cl_env_nested_get(&nest);
918         if (IS_ERR(env))
919                 GOTO(out, result = PTR_ERR(env));
920
921         clob  = ll_i2info(inode)->lli_clob;
922         LASSERT(clob != NULL);
923
924         io = ccc_env_thread_io(env);
925         io->ci_obj = clob;
926         io->ci_ignore_layout = 1;
927         result = cl_io_init(env, io, CIT_MISC, clob);
928         if (result == 0) {
929                 page = cl_page_find(env, clob, vmpage->index,
930                                     vmpage, CPT_CACHEABLE);
931                 if (!IS_ERR(page)) {
932                         lu_ref_add(&page->cp_reference, "writepage",
933                                    current);
934                         cl_page_assume(env, io, page);
935                         result = cl_page_flush(env, io, page);
936                         if (result != 0) {
937                                 /*
938                                  * Re-dirty page on error so it retries write,
939                                  * but not in case when IO has actually
940                                  * occurred and completed with an error.
941                                  */
942                                 if (!PageError(vmpage)) {
943                                         redirty_page_for_writepage(wbc, vmpage);
944                                         result = 0;
945                                         redirtied = true;
946                                 }
947                         }
948                         cl_page_disown(env, io, page);
949                         unlocked = true;
950                         lu_ref_del(&page->cp_reference,
951                                    "writepage", current);
952                         cl_page_put(env, page);
953                 } else {
954                         result = PTR_ERR(page);
955                 }
956         }
957         cl_io_fini(env, io);
958
959         if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
960                 loff_t offset = cl_offset(clob, vmpage->index);
961
962                 /* Flush page failed because the extent is being written out.
963                  * Wait for the write of extent to be finished to avoid
964                  * breaking kernel which assumes ->writepage should mark
965                  * PageWriteback or clean the page. */
966                 result = cl_sync_file_range(inode, offset,
967                                             offset + PAGE_CACHE_SIZE - 1,
968                                             CL_FSYNC_LOCAL, 1);
969                 if (result > 0) {
970                         /* actually we may have written more than one page.
971                          * decreasing this page because the caller will count
972                          * it. */
973                         wbc->nr_to_write -= result - 1;
974                         result = 0;
975                 }
976         }
977
978         cl_env_nested_put(&nest, env);
979         GOTO(out, result);
980
981 out:
982         if (result < 0) {
983                 if (!lli->lli_async_rc)
984                         lli->lli_async_rc = result;
985                 SetPageError(vmpage);
986                 if (!unlocked)
987                         unlock_page(vmpage);
988         }
989         return result;
990 }
991
992 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
993 {
994         struct inode *inode = mapping->host;
995         struct ll_sb_info *sbi = ll_i2sbi(inode);
996         loff_t start;
997         loff_t end;
998         enum cl_fsync_mode mode;
999         int range_whole = 0;
1000         int result;
1001         int ignore_layout = 0;
1002         ENTRY;
1003
1004         if (wbc->range_cyclic) {
1005                 start = mapping->writeback_index << PAGE_CACHE_SHIFT;
1006                 end = OBD_OBJECT_EOF;
1007         } else {
1008                 start = wbc->range_start;
1009                 end = wbc->range_end;
1010                 if (end == LLONG_MAX) {
1011                         end = OBD_OBJECT_EOF;
1012                         range_whole = start == 0;
1013                 }
1014         }
1015
1016         mode = CL_FSYNC_NONE;
1017         if (wbc->sync_mode == WB_SYNC_ALL)
1018                 mode = CL_FSYNC_LOCAL;
1019
1020         if (sbi->ll_umounting)
1021                 /* if the mountpoint is being umounted, all pages have to be
1022                  * evicted to avoid hitting LBUG when truncate_inode_pages()
1023                  * is called later on. */
1024                 ignore_layout = 1;
1025
1026         if (ll_i2info(inode)->lli_clob == NULL)
1027                 RETURN(0);
1028
1029         result = cl_sync_file_range(inode, start, end, mode, ignore_layout);
1030         if (result > 0) {
1031                 wbc->nr_to_write -= result;
1032                 result = 0;
1033          }
1034
1035         if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1036                 if (end == OBD_OBJECT_EOF)
1037                         mapping->writeback_index = 0;
1038                 else
1039                         mapping->writeback_index = (end >> PAGE_CACHE_SHIFT) +1;
1040         }
1041         RETURN(result);
1042 }
1043
1044 struct ll_cl_context *ll_cl_find(struct file *file)
1045 {
1046         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1047         struct ll_cl_context *lcc;
1048         struct ll_cl_context *found = NULL;
1049
1050         read_lock(&fd->fd_lock);
1051         list_for_each_entry(lcc, &fd->fd_lccs, lcc_list) {
1052                 if (lcc->lcc_cookie == current) {
1053                         found = lcc;
1054                         break;
1055                 }
1056         }
1057         read_unlock(&fd->fd_lock);
1058
1059         return found;
1060 }
1061
1062 void ll_cl_add(struct file *file, const struct lu_env *env, struct cl_io *io)
1063 {
1064         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1065         struct ll_cl_context *lcc = &vvp_env_info(env)->vti_io_ctx;
1066
1067         memset(lcc, 0, sizeof(*lcc));
1068         INIT_LIST_HEAD(&lcc->lcc_list);
1069         lcc->lcc_cookie = current;
1070         lcc->lcc_env = env;
1071         lcc->lcc_io = io;
1072
1073         write_lock(&fd->fd_lock);
1074         list_add(&lcc->lcc_list, &fd->fd_lccs);
1075         write_unlock(&fd->fd_lock);
1076 }
1077
1078 void ll_cl_remove(struct file *file, const struct lu_env *env)
1079 {
1080         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1081         struct ll_cl_context *lcc = &vvp_env_info(env)->vti_io_ctx;
1082
1083         write_lock(&fd->fd_lock);
1084         list_del_init(&lcc->lcc_list);
1085         write_unlock(&fd->fd_lock);
1086 }
1087
1088 static int ll_io_read_page(const struct lu_env *env, struct cl_io *io,
1089                            struct cl_page *page)
1090 {
1091         struct inode              *inode  = vvp_object_inode(page->cp_obj);
1092         struct ll_sb_info         *sbi    = ll_i2sbi(inode);
1093         struct ll_file_data       *fd     = vvp_env_io(env)->vui_fd;
1094         struct ll_readahead_state *ras    = &fd->fd_ras;
1095         struct cl_2queue          *queue  = &io->ci_queue;
1096         struct vvp_page           *vpg;
1097         int                        rc = 0;
1098         ENTRY;
1099
1100         vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1101         if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1102             sbi->ll_ra_info.ra_max_pages > 0)
1103                 ras_update(sbi, inode, ras, vvp_index(vpg),
1104                            vpg->vpg_defer_uptodate);
1105
1106         if (vpg->vpg_defer_uptodate) {
1107                 vpg->vpg_ra_used = 1;
1108                 cl_page_export(env, page, 1);
1109         }
1110
1111         cl_2queue_init(queue);
1112         /*
1113          * Add page into the queue even when it is marked uptodate above.
1114          * this will unlock it automatically as part of cl_page_list_disown().
1115          */
1116         cl_2queue_add(queue, page);
1117         if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1118             sbi->ll_ra_info.ra_max_pages > 0) {
1119                 int rc2;
1120
1121                 rc2 = ll_readahead(env, io, &queue->c2_qin, ras,
1122                                    vpg->vpg_defer_uptodate);
1123                 CDEBUG(D_READA, DFID "%d pages read ahead at %lu\n",
1124                        PFID(ll_inode2fid(inode)), rc2, vvp_index(vpg));
1125         }
1126
1127         if (queue->c2_qin.pl_nr > 0)
1128                 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
1129
1130         /*
1131          * Unlock unsent pages in case of error.
1132          */
1133         cl_page_list_disown(env, io, &queue->c2_qin);
1134         cl_2queue_fini(env, queue);
1135
1136         RETURN(rc);
1137 }
1138
1139 int ll_readpage(struct file *file, struct page *vmpage)
1140 {
1141         struct cl_object *clob = ll_i2info(file->f_dentry->d_inode)->lli_clob;
1142         struct ll_cl_context *lcc;
1143         const struct lu_env  *env;
1144         struct cl_io   *io;
1145         struct cl_page *page;
1146         int result;
1147         ENTRY;
1148
1149         lcc = ll_cl_find(file);
1150         if (lcc == NULL) {
1151                 unlock_page(vmpage);
1152                 RETURN(-EIO);
1153         }
1154
1155         env = lcc->lcc_env;
1156         io  = lcc->lcc_io;
1157         LASSERT(io != NULL);
1158         LASSERT(io->ci_state == CIS_IO_GOING);
1159         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
1160         if (!IS_ERR(page)) {
1161                 LASSERT(page->cp_type == CPT_CACHEABLE);
1162                 if (likely(!PageUptodate(vmpage))) {
1163                         cl_page_assume(env, io, page);
1164                         result = ll_io_read_page(env, io, page);
1165                 } else {
1166                         /* Page from a non-object file. */
1167                         unlock_page(vmpage);
1168                         result = 0;
1169                 }
1170                 cl_page_put(env, page);
1171         } else {
1172                 unlock_page(vmpage);
1173                 result = PTR_ERR(page);
1174         }
1175         RETURN(result);
1176 }
1177
1178 int ll_page_sync_io(const struct lu_env *env, struct cl_io *io,
1179                     struct cl_page *page, enum cl_req_type crt)
1180 {
1181         struct cl_2queue  *queue;
1182         int result;
1183
1184         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
1185
1186         queue = &io->ci_queue;
1187         cl_2queue_init_page(queue, page);
1188
1189         result = cl_io_submit_sync(env, io, crt, queue, 0);
1190         LASSERT(cl_page_is_owned(page, io));
1191
1192         if (crt == CRT_READ)
1193                 /*
1194                  * in CRT_WRITE case page is left locked even in case of
1195                  * error.
1196                  */
1197                 cl_page_list_disown(env, io, &queue->c2_qin);
1198         cl_2queue_fini(env, queue);
1199
1200         return result;
1201 }