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