336397773fbb5d911c54081624405818237f13fe
[cascardo/linux.git] / drivers / staging / lustre / 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, 2015, 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 <linux/uaccess.h>
49
50 #include <linux/fs.h>
51 #include <linux/pagemap.h>
52 /* current_is_kswapd() */
53 #include <linux/swap.h>
54
55 #define DEBUG_SUBSYSTEM S_LLITE
56
57 #include "../include/lustre_lite.h"
58 #include "../include/obd_cksum.h"
59 #include "llite_internal.h"
60 #include "../include/linux/lustre_compat25.h"
61
62 /**
63  * Finalizes cl-data before exiting typical address_space operation. Dual to
64  * ll_cl_init().
65  */
66 void ll_cl_fini(struct ll_cl_context *lcc)
67 {
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);
73         LASSERT(env);
74
75         if (page) {
76                 lu_ref_del(&page->cp_reference, "cl_io", io);
77                 cl_page_put(env, page);
78         }
79
80         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  */
87 struct ll_cl_context *ll_cl_init(struct file *file, struct page *vmpage)
88 {
89         struct ll_cl_context *lcc;
90         struct lu_env    *env;
91         struct cl_io     *io;
92         struct cl_object *clob;
93         struct vvp_io    *vio;
94
95         int refcheck;
96         int result = 0;
97
98         clob = ll_i2info(file_inode(file))->lli_clob;
99         LASSERT(clob);
100
101         env = cl_env_get(&refcheck);
102         if (IS_ERR(env))
103                 return ERR_CAST(env);
104
105         lcc = &ll_env_info(env)->lti_io_ctx;
106         memset(lcc, 0, sizeof(*lcc));
107         lcc->lcc_env = env;
108         lcc->lcc_refcheck = refcheck;
109         lcc->lcc_cookie = current;
110
111         vio = vvp_env_io(env);
112         io = vio->vui_cl.cis_io;
113         lcc->lcc_io = io;
114         if (!io)
115                 result = -EIO;
116
117         if (result == 0 && vmpage) {
118                 struct cl_page   *page;
119
120                 LASSERT(io->ci_state == CIS_IO_GOING);
121                 LASSERT(vio->vui_fd == LUSTRE_FPRIVATE(file));
122                 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;
128                 } else {
129                         result = PTR_ERR(page);
130                 }
131         }
132         if (result) {
133                 ll_cl_fini(lcc);
134                 lcc = ERR_PTR(result);
135         }
136
137         return lcc;
138 }
139
140 static 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
159  * get a zero ra window, although there is still ra space remaining. - Jay
160  */
161 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
162                                      struct ra_io_arg *ria,
163                                      unsigned long pages, unsigned long min)
164 {
165         struct ll_ra_info *ra = &sbi->ll_ra_info;
166         long ret;
167
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
170          * performance a lot.
171          */
172         ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages), pages);
173         if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages)) {
174                 ret = 0;
175                 goto out;
176         }
177
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
188          * the RPC boundary from needing an extra read RPC.
189          */
190         if (ria->ria_pages == 0) {
191                 long beyond_rpc = (ria->ria_start + ret) % PTLRPC_MAX_BRW_PAGES;
192
193                 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
202 out:
203         if (ret < min) {
204                 /* override ra limit for maximum performance */
205                 atomic_add(min - ret, &ra->ra_cur_pages);
206                 ret = min;
207         }
208         return ret;
209 }
210
211 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
212 {
213         struct ll_ra_info *ra = &sbi->ll_ra_info;
214
215         atomic_sub(len, &ra->ra_cur_pages);
216 }
217
218 static 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
224 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
225 {
226         struct ll_sb_info *sbi = ll_i2sbi(inode);
227
228         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"    \
234                "csr %lu sf %lu sp %lu sl %lu\n",                            \
235                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
242 static 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)
248                 start = 0;
249         if (end < point)
250                 end = ~0;
251
252         return start <= index && index <= end;
253 }
254
255 void ll_ras_enter(struct file *f)
256 {
257         struct ll_file_data *fd = LUSTRE_FPRIVATE(f);
258         struct ll_readahead_state *ras = &fd->fd_ras;
259
260         spin_lock(&ras->ras_lock);
261         ras->ras_requests++;
262         ras->ras_request_index = 0;
263         ras->ras_consecutive_requests++;
264         spin_unlock(&ras->ras_lock);
265 }
266
267 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
268                               struct cl_page_list *queue, struct cl_page *page,
269                               struct cl_object *clob, pgoff_t *max_index)
270 {
271         struct page *vmpage = page->cp_vmpage;
272         struct vvp_page *vpg;
273         int           rc;
274
275         rc = 0;
276         cl_page_assume(env, io, page);
277         lu_ref_add(&page->cp_reference, "ra", current);
278         vpg = cl2vvp_page(cl_object_page_slice(clob, page));
279         if (!vpg->vpg_defer_uptodate && !PageUptodate(vmpage)) {
280                 CDEBUG(D_READA, "page index %lu, max_index: %lu\n",
281                        vvp_index(vpg), *max_index);
282                 if (*max_index == 0 || vvp_index(vpg) > *max_index)
283                         rc = cl_page_is_under_lock(env, io, page, max_index);
284                 if (rc == 0) {
285                         vpg->vpg_defer_uptodate = 1;
286                         vpg->vpg_ra_used = 0;
287                         cl_page_list_add(queue, page);
288                         rc = 1;
289                 } else {
290                         cl_page_discard(env, io, page);
291                         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);
299         return rc;
300 }
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  */
312 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
313                               struct cl_page_list *queue,
314                               pgoff_t index, pgoff_t *max_index)
315 {
316         struct cl_object *clob  = io->ci_obj;
317         struct inode     *inode = vvp_object_inode(clob);
318         struct page      *vmpage;
319         struct cl_page   *page;
320         enum ra_stat      which = _NR_RA_STAT; /* keep gcc happy */
321         int            rc    = 0;
322         const char       *msg   = NULL;
323
324         vmpage = grab_cache_page_nowait(inode->i_mapping, index);
325         if (vmpage) {
326                 /* Check if vmpage was truncated or reclaimed */
327                 if (vmpage->mapping == inode->i_mapping) {
328                         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,
332                                                         page, clob, max_index);
333                                 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);
347                 put_page(vmpage);
348         } else {
349                 which = RA_STAT_FAILED_GRAB_PAGE;
350                 msg   = "g_c_p_n failed";
351         }
352         if (msg) {
353                 ll_ra_stats_inc(inode, which);
354                 CDEBUG(D_READA, "%s\n", msg);
355         }
356         return rc;
357 }
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
367  * striped over, rather than having a constant value for all files here.
368  */
369
370 /* RAS_INCREASE_STEP should be (1UL << (inode->i_blkbits - PAGE_SHIFT)).
371  * Temporarily set RAS_INCREASE_STEP to 1MB. After 4MB RPC is enabled
372  * 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
374  * up quickly which will affect read performance significantly. See LU-2816
375  */
376 #define RAS_INCREASE_STEP(inode) (ONE_MB_BRW_SIZE >> PAGE_SHIFT)
377
378 static inline int stride_io_mode(struct ll_readahead_state *ras)
379 {
380         return ras->ras_consecutive_stride_requests > 1;
381 }
382
383 /* The function calculates how much pages will be read in
384  * [off, off + length], in such stride IO area,
385  * stride_offset = st_off, stride_length = st_len,
386  * 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  */
401 static unsigned long
402 stride_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
424         CDEBUG(D_READA, "start %llu, end %llu start_left %lu end_left %lu\n",
425                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
432         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);
434
435         return pg_count;
436 }
437
438 static 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 */
449 static 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
454          * the ria_pages.
455          */
456         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
461 static 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,
465                                unsigned long *ra_end)
466 {
467         int rc, count = 0;
468         bool stride_ria;
469         pgoff_t page_idx;
470         pgoff_t max_index = 0;
471
472         LASSERT(ria);
473         RIA_DEBUG(ria);
474
475         stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
476         for (page_idx = ria->ria_start;
477              page_idx <= ria->ria_end && *reserved_pages > 0; page_idx++) {
478                 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,
481                                                 page_idx, &max_index);
482                         if (rc == 1) {
483                                 (*reserved_pages)--;
484                                 count++;
485                         } else if (rc == -ENOLCK) {
486                                 break;
487                         }
488                 } 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
491                          * the stride gap
492                          */
493                         pgoff_t offset;
494                         /* FIXME: This assertion only is valid when it is for
495                          * forward read-ahead, it will be fixed when backward
496                          * read-ahead is implemented
497                          */
498                         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);
502                         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;
506                                 CDEBUG(D_READA, "i %lu skip %lu\n", page_idx,
507                                        ria->ria_length - offset);
508                                 continue;
509                         }
510                 }
511         }
512         *ra_end = page_idx;
513         return count;
514 }
515
516 int ll_readahead(const struct lu_env *env, struct cl_io *io,
517                  struct cl_page_list *queue, struct ll_readahead_state *ras,
518                  bool hit)
519 {
520         struct vvp_io *vio = vvp_env_io(env);
521         struct ll_thread_info *lti = ll_env_info(env);
522         struct cl_attr *attr = vvp_env_thread_attr(env);
523         unsigned long start = 0, end = 0, reserved;
524         unsigned long ra_end, len, mlen = 0;
525         struct inode *inode;
526         struct ra_io_arg *ria = &lti->lti_ria;
527         struct cl_object *clob;
528         int ret = 0;
529         __u64 kms;
530
531         clob = io->ci_obj;
532         inode = vvp_object_inode(clob);
533
534         memset(ria, 0, sizeof(*ria));
535
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)
541                 return ret;
542         kms = attr->cat_kms;
543         if (kms == 0) {
544                 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
545                 return 0;
546         }
547
548         spin_lock(&ras->ras_lock);
549
550         /* Enlarge the RA window to encompass the full read */
551         if (vio->vui_ra_valid &&
552             ras->ras_window_start + ras->ras_window_len <
553             vio->vui_ra_start + vio->vui_ra_count) {
554                 ras->ras_window_len = vio->vui_ra_start + vio->vui_ra_count -
555                                       ras->ras_window_start;
556         }
557
558         /* 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
575                  * random reading.
576                  */
577                 rpc_boundary = (end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1));
578                 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 */
585                 end = min(end, (unsigned long)((kms - 1) >> PAGE_SHIFT));
586
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) {
601                 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
602                 return 0;
603         }
604         len = ria_page_count(ria);
605         if (len == 0) {
606                 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
607                 return 0;
608         }
609
610         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,
613                vio->vui_ra_valid ? vio->vui_ra_start : 0,
614                vio->vui_ra_valid ? vio->vui_ra_count : 0,
615                hit);
616
617         /* at least to extend the readahead window to cover current read */
618         if (!hit && vio->vui_ra_valid &&
619             vio->vui_ra_start + vio->vui_ra_count > ria->ria_start) {
620                 /* to the end of current read window. */
621                 mlen = vio->vui_ra_start + vio->vui_ra_count - ria->ria_start;
622                 /* 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);
628         if (reserved < len)
629                 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
630
631         CDEBUG(D_READA, "reserved pages %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
632                reserved, len, mlen,
633                atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
634                ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
635
636         ret = ll_read_ahead_pages(env, io, queue, ria, &reserved, &ra_end);
637
638         if (reserved != 0)
639                 ll_ra_count_put(ll_i2sbi(inode), reserved);
640
641         if (ra_end == end + 1 && ra_end == (kms >> PAGE_SHIFT))
642                 ll_ra_stats_inc(inode, RA_STAT_EOF);
643
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
648          * of the app and behind the next index to start read-ahead from
649          */
650         CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu\n",
651                ra_end, end, ria->ria_end);
652
653         if (ra_end != end + 1) {
654                 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
655                 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
665         return ret;
666 }
667
668 static 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 */
675 static 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 */
689 static 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
697 void 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;
702 }
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  */
708 static 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
728 static 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 ||
734             ras->ras_consecutive_stride_requests == 0)) {
735                 ras->ras_stride_pages = ras->ras_consecutive_pages;
736                 ras->ras_stride_length = stride_gap+ras->ras_consecutive_pages;
737         }
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;
748         ras->ras_stride_length = stride_gap+ras->ras_consecutive_pages;
749
750         RAS_CDEBUG(ras);
751         return;
752 }
753
754 /* Stride Read-ahead window will be increased inc_len according to
755  * stride I/O pattern
756  */
757 static 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
766                  >= ras->ras_stride_offset, "window_start %lu, window_len %lu stride_offset %lu\n",
767                  ras->ras_window_start,
768                  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
788         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)
791                 ras->ras_window_len = window_len;
792
793         RAS_CDEBUG(ras);
794 }
795
796 static 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
812 void 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;
818
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
827          * reclaiming it before we get to it.
828          */
829         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
845          * ras_requests and thus can never trigger this behavior.
846          */
847         if (ras->ras_requests == 2 && !ras->ras_request_index) {
848                 __u64 kms_pages;
849
850                 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
851                             PAGE_SHIFT;
852
853                 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
854                        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);
863                         goto out_unlock;
864                 }
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++;
878                         goto out_unlock;
879                 } 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
892                                  *redetecting read-ahead mode
893                                  */
894                                 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
900                                  * window
901                                  */
902                                 ras_reset(inode, ras, index);
903                                 ras->ras_consecutive_pages++;
904                                 ras_stride_reset(ras);
905                                 goto out_unlock;
906                         }
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,
910                          * if invalid, it will reset the stride ra window
911                          */
912                         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
924         if (stride_io_mode(ras)) {
925                 /* Since stride readahead is sensitive to the offset
926                  * of read-ahead, so we use original offset here,
927                  * instead of ras_window_start, which is RPC aligned
928                  */
929                 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
930         } 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         }
936         RAS_CDEBUG(ras);
937
938         /* Trigger RA in the mmap case where ras_consecutive_requests
939          * is not incremented and thus can't be used to trigger RA
940          */
941         if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
942                 ras->ras_window_len = RAS_INCREASE_STEP(inode);
943                 goto out_unlock;
944         }
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);
963 out_unlock:
964         RAS_CDEBUG(ras);
965         ras->ras_request_index++;
966         spin_unlock(&ras->ras_lock);
967         return;
968 }
969
970 int 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;
982
983         LASSERT(PageLocked(vmpage));
984         LASSERT(!PageWriteback(vmpage));
985
986         LASSERT(ll_i2dtexp(inode));
987
988         env = cl_env_nested_get(&nest);
989         if (IS_ERR(env)) {
990                 result = PTR_ERR(env);
991                 goto out;
992         }
993
994         clob  = ll_i2info(inode)->lli_clob;
995         LASSERT(clob);
996
997         io = vvp_env_thread_io(env);
998         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
1038                  * PageWriteback or clean the page.
1039                  */
1040                 result = cl_sync_file_range(inode, offset,
1041                                             offset + PAGE_SIZE - 1,
1042                                             CL_FSYNC_LOCAL, 1);
1043                 if (result > 0) {
1044                         /* actually we may have written more than one page.
1045                          * decreasing this page because the caller will count
1046                          * it.
1047                          */
1048                         wbc->nr_to_write -= result - 1;
1049                         result = 0;
1050                 }
1051         }
1052
1053         cl_env_nested_put(&nest, env);
1054         goto out;
1055
1056 out:
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
1067 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1068 {
1069         struct inode *inode = mapping->host;
1070         struct ll_sb_info *sbi = ll_i2sbi(inode);
1071         loff_t start;
1072         loff_t end;
1073         enum cl_fsync_mode mode;
1074         int range_whole = 0;
1075         int result;
1076         int ignore_layout = 0;
1077
1078         if (wbc->range_cyclic) {
1079                 start = mapping->writeback_index << PAGE_SHIFT;
1080                 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
1094         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()
1097                  * is called later on.
1098                  */
1099                 ignore_layout = 1;
1100         result = cl_sync_file_range(inode, start, end, mode, ignore_layout);
1101         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)
1108                         mapping->writeback_index = 0;
1109                 else
1110                         mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
1111         }
1112         return result;
1113 }
1114
1115 int ll_readpage(struct file *file, struct page *vmpage)
1116 {
1117         struct ll_cl_context *lcc;
1118         int result;
1119
1120         lcc = ll_cl_init(file, vmpage);
1121         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         }
1140         return result;
1141 }
1142
1143 int 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 }