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